configuratorware/configurator-api
Installation
composer require configuratorware/configurator-api
Add the service provider to config/app.php:
'providers' => [
// ...
Configuratorware\ConfiguratorApi\ConfiguratorApiServiceProvider::class,
],
Publish Config
php artisan vendor:publish --provider="Configuratorware\ConfiguratorApi\ConfiguratorApiServiceProvider" --tag="config"
This generates config/configurator-api.php. Update with your API endpoint, API key, and default options.
First Use Case: Fetching a Product Configuration
use Configuratorware\ConfiguratorApi\Facades\ConfiguratorApi;
$productId = 'prod_123';
$config = ConfiguratorApi::getProductConfig($productId);
// Handle response (success/error)
if ($config->isSuccess()) {
$data = $config->getData();
// Process configuration data
} else {
$error = $config->getError();
// Log or handle error
}
api_endpoint: Base URL for the configurator API.api_key: Authentication key (use environment variables for security).timeout: Default request timeout (e.g., 30 seconds).debug: Enable/disable debug logging.Verify the API connection with:
php artisan configurator-api:test
This runs a health check against the configured endpoint.
// Fetch full configuration for a product
$config = ConfiguratorApi::getProductConfig($productId);
// Fetch specific rules (e.g., pricing, compatibility)
$rules = ConfiguratorApi::getProductRules($productId, ['pricing', 'compatibility']);
Validate user selections before submission:
$selections = [
'option_1' => 'value_a',
'option_2' => 'value_b',
];
$validation = ConfiguratorApi::validateSelections($productId, $selections);
if ($validation->isValid()) {
// Proceed to checkout
} else {
$errors = $validation->getErrors();
// Show errors to user
}
Leverage Laravel’s cache to reduce API calls:
$config = Cache::remember("config_{$productId}", now()->addHours(1), function () use ($productId) {
return ConfiguratorApi::getProductConfig($productId);
});
Use Laravel events to sync configuration changes:
// In a service or controller
event(new \Configuratorware\ConfiguratorApi\Events\ConfigUpdated($productId, $newConfig));
// Listen in EventServiceProvider
protected $listen = [
\Configuratorware\ConfiguratorApi\Events\ConfigUpdated::class => [
\App\Listeners\SyncProductConfig::class,
],
];
Attach configurations to a Product model:
namespace App\Models;
use Configuratorware\ConfiguratorApi\Facades\ConfiguratorApi;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function getConfiguration()
{
return Cache::remember("config_{$this->id}", now()->addHours(1), function () {
return ConfiguratorApi::getProductConfig($this->id);
});
}
}
ConfiguratorApi::dispatchSyncProductConfig($productId)->onQueue('configurator');
Use Laravel’s retry mechanism for transient failures:
$config = ConfiguratorApi::withRetry(3, 1000)->getProductConfig($productId);
Listen for configuration updates via webhooks:
Route::post('/config-webhook', function (Request $request) {
$payload = $request->json()->all();
ConfiguratorApi::handleWebhook($payload);
});
Mock the API in tests using Laravel’s HTTP testing:
$response = Http::fake([
'configurator-api.example.com/*' => Http::response(['data' => 'mock'], 200),
]);
$config = ConfiguratorApi::getProductConfig('test_123');
$config->assertSuccess();
config/configurator-api.php..env:
CONFIGURATOR_API_KEY=your_secure_key_here
Then reference it in config:
'api_key' => env('CONFIGURATOR_API_KEY'),
Cache::forget() in webhook listeners.'timeout' => 60, // seconds
ConfiguratorApi::withRetry(5, 2000)->getProductConfig($productId);
$data = $config->getData() ?? [];
$price = $data['price'] ?? null;
'debug' => env('APP_DEBUG', false),
Logs requests/responses to storage/logs/configurator-api.log.
$response = ConfiguratorApi::getProductConfig($productId);
if ($response->isError()) {
\Log::debug('Raw response:', $response->getRawResponse());
}
| Code | Description | Solution |
|---|---|---|
| 401 | Invalid API key | Check .env and API key permissions. |
| 404 | Product not found | Validate $productId format. |
| 429 | Rate limit exceeded | Implement retries or queue the request. |
| 500 | Server error | Contact API support; check logs. |
Override response handling:
ConfiguratorApi::extend(function ($client) {
$client->extend('transform', function ($response) {
$data = $response->json();
return collect($data)->when(
$data['type'] === 'error',
fn ($c) => $c->merge(['custom_field' => 'value'])
);
});
});
Add headers or modify requests globally:
ConfiguratorApi::extend(function ($client) {
$client->getMiddleware()->prepend(
\Configuratorware\ConfiguratorApi\Middleware\AddCustomHeader::class
);
});
Listen for API events to trigger custom logic:
ConfiguratorApi::on('config.fetched', function ($productId, $config) {
// Trigger analytics or sync to another service
});
Override API endpoints for local development:
if (app()->environment('local')) {
ConfiguratorApi::setEndpoint('http://configurator-api.local');
}
How can I help you explore Laravel packages today?