/symfony-hexagonal:symfony-cqrs-handlersfrom symfony-hexagonal
Guides implementing CQRS with Symfony Messenger: commands, queries, handlers, typed bus wrappers, and bus configuration for Symfony 6.4-8.x.
Ships in symfony-hexagonal. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
You are an expert in CQRS (Command Query Responsibility Segregation) within Symfony hexagonal architecture.
Symfony's docs recommend a single bus unless you need different middleware. This architecture has that need: command.bus wraps handlers in doctrine_transaction, query.bus must never open write transactions, and event.bus must allow zero handlers. Mention this when users question the extra buses.
| Feature | Requires | Fallback |
|---|---|---|
#[AsMessageHandler(bus: '...')] | Symfony 6.0 | - |
#[AsMessage('async')] on a command | Symfony 7.2 | framework.messenger.routing in YAML |
DeduplicateStamp | Symfony 7.3 + symfony/lock | Idempotency key check in the handler |
#[AsMessage(serializedTypeName: ...)] | Symfony 8.1 | default FQCN type name |
Commands represent write operations (create, update, delete). They are messages dispatched to the command bus.
final readonly class, immutable after constructionRegisterUser, PlaceOrder, CancelSubscriptionvoid or a scalar identifier (string ID)namespace App\Application\{Module}\Command;
use Symfony\Component\Validator\Constraints as Assert;
final readonly class {ActionVerb}{Entity}
{
public function __construct(
#[Assert\NotBlank]
public string $param1,
public string $param2,
) {
}
}
namespace App\Application\{Module}\Command;
use App\Domain\{Module}\Port\{Entity}RepositoryInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler(bus: 'command.bus')]
final readonly class {ActionVerb}{Entity}Handler
{
public function __construct(
private {Entity}RepositoryInterface $repository,
) {
}
public function __invoke({ActionVerb}{Entity} $command): void
{
// 1. Load/create domain objects
// 2. Execute business logic (entity methods)
// 3. Persist via port (repository dispatches domain events)
// NO side-effects here, use domain events
}
}
Queries represent read operations. They return DTOs, never domain entities.
final readonly class, immutableGetUserById, ListActiveOrders, SearchProductsPage of DTOs{Entity}ReadRepositoryInterface) backed by DBAL QueryBuildernamespace App\Application\{Module}\Query;
final readonly class Get{Entity}ById
{
public function __construct(
public string $id,
) {
}
}
namespace App\Application\{Module}\Query;
use App\Application\{Module}\DTO\{Entity}DTO;
use App\Domain\{Module}\Port\{Entity}RepositoryInterface;
use App\Domain\{Module}\ValueObject\{Entity}Id;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler(bus: 'query.bus')]
final readonly class Get{Entity}ByIdHandler
{
public function __construct(
private {Entity}RepositoryInterface $repository,
) {
}
public function __invoke(Get{Entity}ById $query): ?{Entity}DTO
{
$entity = $this->repository->findById(new {Entity}Id($query->id));
return $entity === null ? null : {Entity}DTO::fromEntity($entity);
}
}
namespace App\Application\{Module}\DTO;
use App\Domain\{Module}\Entity\{Entity};
final readonly class {Entity}DTO
{
public function __construct(
public string $id,
public string $name,
) {
}
public static function fromEntity({Entity} $entity): self
{
return new self(
id: $entity->id()->value,
name: $entity->name(),
);
}
}
Symfony 7.3+ ObjectMapper (#[Map]) can replace hand-written fromX() factories for DTO-to-DTO mapping. Do not map directly into Domain entities with it: it bypasses constructors and therefore invariants.
Use the CommandBus / QueryBus wrappers from references/bus-configuration.md. They return the handler result and rethrow the original domain exception instead of Messenger's HandlerFailedException, so the API exception subscriber can map it to the right HTTP status.
See references/ for detailed guides:
command-patterns.md - Full command examples with validation, async commandsquery-patterns.md - Read ports, pagination, DBAL QueryBuilder read modelsbus-configuration.md - Messenger bus setup, middleware, bus wrappersWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
cqrs, symfony-messenger, hexagonal-architecture, command-bus, query-bus
10 skills in this plugin
/symfony-api-responseSymfony REST API response standard {result, error, extra, status}: thin controllers, #[MapRequestPayload] / #[MapQueryString] request DTOs, ApiResponseTrait, exception subscriber that unwraps Messenger HandlerFailedException, validation error format, debug mode. Use when: API, endpoint, controller, response, error handling, JSON, REST, API response, exception subscriber, HTTP, request DTO/symfony-doctrine-persistenceDoctrine persistence for Symfony hexagonal architecture (ORM 2.x/3.x, DBAL 3/4, DoctrineBundle 2/3): repository adapters, XML mapping in Infrastructure, embeddables, enums, custom DBAL types, native lazy objects, migrations, transactions, read models with DBAL QueryBuilder, strict no-native-SQL rule. Use when: doctrine, repository, persistence, database, mapping, migration, ORM, entity manager, DBAL, transaction, query builder, pagination/symfony-domain-modelingSymfony DDD domain modeling in pure PHP: entities, aggregates, value objects, domain events, domain exceptions, ID generation ports, PHP 8.2-8.5 modeling features (readonly, private(set), clone with). Use when: entity, value object, domain event, aggregate, domain exception, domain model, domain logic, business rule, invariant, enum status/symfony-hexagonal-architectureSymfony hexagonal architecture (ports & adapters) setup for Symfony 6.4 LTS, 7.4 LTS and 8.x: project structure, module scaffolding, layer responsibilities, dependency rules, version compatibility, progressive refactoring of legacy code. Use when: architecture, module, layer, hexagonal, scaffold, project structure, directory structure, new project, new module, bounded context, deptrac, Symfony version, upgrade/symfony-messenger-asyncSymfony Messenger async processing and Symfony Scheduler instead of cron: transports, routing (#[AsMessage] 7.2+), retry with backoff and jitter, failure transport, idempotency keys, DeduplicateStamp (7.3+), workers, supervisor, scheduled commands with stateful/locked schedules. Use when: messenger, async, queue, retry, scheduler, background job, worker, transport, message queue, cron, scheduled task, idempotency, deduplication/symfony-ports-adaptersSymfony ports and adapters: port interfaces in Domain, adapter implementations in Infrastructure, dependency injection, autowiring, services.yaml aliases, #[AsAlias], environment-specific and decorated adapters, in-memory test adapters. Use when: port, adapter, interface, repository interface, DI, autowiring, dependency injection, services.yaml, binding, decorator, AsAliasnpx claudepluginhub aligundogdu/symfony-hexagonal-skill --plugin symfony-hexagonalOther plugins' skills for the same job