Installation
composer require commercetools/symfony-bundle
Ensure composer config extra.symfony.allow-contrib true is set (Symfony Flex requirement).
Configuration
Add the bundle to config/bundles.php:
return [
// ...
Commercetools\ClientBundle\CommercetoolsClientBundle::class => ['all' => true],
Commercetools\SdkBundle\CommercetoolsSdkBundle::class => ['all' => true],
];
Environment Setup
Define credentials in .env:
COMMERCETOOLS_CLIENT_ID=your_client_id
COMMERCETOOLS_CLIENT_SECRET=your_client_secret
COMMERCETOOLS_PROJECT_KEY=your_project_key
COMMERCETOOLS_API_URL=https://api.europe-west1.gcp.commercetools.com
COMMERCETOOLS_TOKEN_URL=https://auth.europe-west1.gcp.commercetools.com
First Use Case Inject the client into a service/controller:
use Commercetools\Client\ClientBuilder;
use Commercetools\Client\Http\Middleware\AuthMiddleware;
public function __construct(private ClientBuilder $clientBuilder) {}
public function fetchProducts(): array {
$client = $this->clientBuilder->buildClient();
$products = $client->search()->products()->post(['query' => '*' ]);
return $products->getBody()['results'];
}
Service Integration
Use dependency injection for the ClientBuilder and HttpClient:
// services.yaml
services:
App\Service\ProductService:
arguments:
$clientBuilder: '@commercetools.client_builder'
API Calls Wrap SDK calls in domain services to abstract complexity:
class ProductService {
public function getProductById(string $id): ?Product {
return $this->clientBuilder->buildClient()
->withRequestExecutor(new RequestExecutor())
->getById(Product::class, $id);
}
}
Twig Integration Pass SDK models directly to Twig templates:
{% for product in products %}
<h2>{{ product.name.en }}</h2>
<p>{{ product.price.value | currency }}</p>
{% endfor %}
Console Commands Leverage built-in commands for bulk operations:
php bin/console commercetools:products:import ./data/products.json
$cache = $this->container->get('commercetools.cache');
$cachedProducts = $cache->get('products', function() use ($client) {
return $client->search()->products()->post(['query' => '*']);
});
$client = $this->clientBuilder->buildClient()
->withMiddleware(new ErrorHandlingMiddleware());
Authentication Issues
.env variables are correctly set and the token URL matches your region.php bin/console debug:container commercetools.auth_middleware
SDK Version Mismatch
composer require commercetools/sdk:^x.y.z commercetools/symfony-bundle:^x.y.z
Rate Limiting
$client->withMiddleware(new RateLimitMiddleware());
Twig Template Errors
@commercetools.twig.model_serializer:
{{ dump(product | commercetools_model_serializer) }}
Log API Requests
Enable debug mode in config/packages/commercetools.yaml:
commercetools:
debug: true
Logs will appear in var/log/dev.log.
Validate Payloads
Use the Validator service to validate SDK models before submission:
$validator = $this->container->get('validator');
$errors = $validator->validate($product);
Custom Middleware Extend the client with custom middleware (e.g., logging, retries):
$client->withMiddleware(new CustomLoggingMiddleware());
Event Subscribers
Subscribe to SDK events (e.g., ProductCreatedEvent) for real-time updates:
use Commercetools\Sdk\Event\EventSubscriberInterface;
class ProductEventSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
'product.created' => 'onProductCreated',
];
}
}
Custom Console Commands Extend the base command classes for project-specific logic:
use Commercetools\SdkBundle\Command\AbstractCommand;
class CustomProductCommand extends AbstractCommand {
protected function configure(): void {
$this->setName('commercetools:products:custom');
}
}
How can I help you explore Laravel packages today?