/symfony-skills:hexagonal-architecturefrom symfony-skills
Structures Symfony apps with ports & adapters / clean architecture, keeping the domain framework-free. Use when the task mentions hexagonal, ports/adapters, or "keep the domain pure".
Ships in symfony-skills. 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.
None: SKILL.md is the whole skill.
When to reach for this. Hexagonal is for complex, long-lived domains where isolating business logic from the framework pays off — rich invariants, multiple delivery mechanisms, logic you expect to outlive the current stack. For typical CRUD and feature work (the ~80% of most apps), use
layered-architectureinstead; strict ports/adapters there is just overhead. Apply this to the gnarly 20% that earns it, not as a blanket rule.
┌─────────────────────────────────────────┐
│ Infrastructure │ ← Symfony, Doctrine, HTTP, Messenger
│ (driving + driven adapters) │
│ ┌───────────────────────────────────┐ │
│ │ Application │ │ ← use cases / handlers
│ │ ┌───────────────────────────┐ │ │
│ │ │ Domain │ │ │ ← entities, value objects, ports
│ │ │ (no framework imports) │ │ │
│ │ └───────────────────────────┘ │ │
│ └───────────────────────────────────┘ │
└─────────────────────────────────────────┘
Source code dependencies point inward. The domain knows nothing about Symfony, Doctrine, or HTTP. Outer layers depend on inner-layer interfaces, never the reverse.
src/
├── Domain/ # pure PHP — no Symfony/Doctrine "use" statements
│ ├── Order/
│ │ ├── Order.php # aggregate root (plain object, no #[ORM\...])
│ │ ├── OrderId.php # value object
│ │ ├── OrderStatus.php # enum
│ │ └── OrderRepository.php # PORT (interface)
├── Application/ # use cases — orchestrates domain via ports
│ └── Order/
│ ├── CreateOrderHandler.php
│ └── CreateOrderCommand.php
└── Infrastructure/ # adapters — implement ports using Symfony/Doctrine
├── Persistence/Doctrine/
│ ├── DoctrineOrderRepository.php # ADAPTER implements Domain\Order\OrderRepository
│ └── Mapping/Order.orm.xml # ORM mapping kept OUT of the domain class
└── Http/
└── CreateOrderController.php # driving adapter
A port is an interface the domain owns and the infrastructure implements.
// ✅ src/Domain/Order/OrderRepository.php — no framework imports
namespace App\Domain\Order;
interface OrderRepository
{
public function save(Order $order): void;
public function ofId(OrderId $id): ?Order;
}
// ✅ src/Infrastructure/Persistence/Doctrine/DoctrineOrderRepository.php
namespace App\Infrastructure\Persistence\Doctrine;
use App\Domain\Order\Order;
use App\Domain\Order\OrderId;
use App\Domain\Order\OrderRepository;
use Doctrine\ORM\EntityManagerInterface;
final class DoctrineOrderRepository implements OrderRepository
{
public function __construct(private readonly EntityManagerInterface $em) {}
public function save(Order $order): void
{
$this->em->persist($order);
$this->em->flush();
}
public function ofId(OrderId $id): ?Order
{
return $this->em->find(Order::class, $id->toString());
}
}
Wire the interface to the implementation in config/services.yaml:
services:
App\Domain\Order\OrderRepository: '@App\Infrastructure\Persistence\Doctrine\DoctrineOrderRepository'
The hard part in Symfony is that Doctrine attribute mapping (#[ORM\Entity]) couples the entity to the ORM. Two acceptable strategies:
Infrastructure/Persistence/Doctrine/Mapping/. The domain never imports Doctrine.// ✅ STRICT — pure aggregate, mapped externally via XML
namespace App\Domain\Order;
final class Order
{
/** @var Collection<int, OrderLine>|OrderLine[] */
private array $lines = [];
private function __construct(
private readonly OrderId $id,
private OrderStatus $status,
) {}
public static function place(OrderId $id): self
{
return new self($id, OrderStatus::Pending); // invariant: new orders start Pending
}
}
State the chosen strategy in the project README so the agent stays consistent.
One handler per use case. It depends only on ports, never on adapters.
// ✅ src/Application/Order/CreateOrderHandler.php
namespace App\Application\Order;
use App\Domain\Order\Order;
use App\Domain\Order\OrderId;
use App\Domain\Order\OrderRepository;
final readonly class CreateOrderHandler
{
public function __construct(private OrderRepository $orders) {}
public function __invoke(CreateOrderCommand $command): OrderId
{
$order = Order::place(OrderId::generate());
$this->orders->save($order);
return $order->id();
}
}
Controllers, console commands, and Messenger handlers are driving adapters. They translate a delivery mechanism into an application call and translate the result back out. They contain no business logic.
EntityManagerInterface into the application/domain layer — depend on a repository port instead.Symfony\… or Doctrine\… in src/Domain/ — the domain must stay framework-free (or use the agreed pragmatic exception for ORM attributes only).DoctrineOrderRepository — depend on the interface; wire it in services.yaml.Response from a use-case handler — return a domain value (ID, DTO); the controller builds the HTTP response.#[ORM\Entity] on a "pure" domain class when the project chose the strict XML-mapping strategy — check the README's stated strategy first.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skills17 skills in this plugin
/api-platform-resourcesUse when exposing data through API Platform 4 — defining #[ApiResource], operations, DTO resources, state providers/processors, serialization groups, filters, and pagination. Use when the task mentions API Platform, ApiResource, state processor, or exposing an entity as a REST/GraphQL endpoint.
/bundle-organizationUse when structuring a Symfony project's src/ directory, organizing code into modules, configuring services, or deciding where a class belongs. Covers the modular monolith layout, service configuration, and config/ conventions for Symfony 7 (Flex, no app bundle).
/console-commandsUse when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.
Other plugins' skills for the same job