Decide where code goes in a Symfony project and wire it correctly: the Controller / Service / Repository / DTO / Entity contract, the three DTO namespaces (Input, Read, Output), service design and who calls flush(), business exceptions carrying their own HTTP status, direct calls instead of domain events, configuration split between env vars, app. parameters and class constants, and the dependency-injection attributes (#[Autowire], #[AutowireIterator], #[AutowireLocator], #[AsDecorator], #[AsTaggedItem], #[Target], #[When], #[Lazy]). Use this skill whenever someone asks where a piece of logic belongs, wants a new service or DTO, says a controller has grown too big, wants to extract or move a class that has outgrown its place, asks how to make something configurable or add a setting or an env var, asks why a service is not autowired or how to inject one of several implementations, wants a factory, strategy, decorator or object mapping, or asks "is this the right place for this?", "how should I structure this feature?", "where does the rating logic go?", "extract this", "why does my exception return a 500?". Also use it for the list of alternatives this standard deliberately rejected and why. For a feature described in plain language, or a bare "refactor this" with no target, `symfony-yoandev-standards` routes first.
Trending skills
7-day visitors to each skill page
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.
Read when SKILL.md points to them.
Tier: core — the layer contract and the five rules live here, and none of them is optional. The one on-demand item is the deptrac configuration that enforces it mechanically (
symfony-yoandev-quality).
Where code goes, and why the boundary is where it is.
One rule generates most of the others: the entity holds no rule, so the service holds every rule. Everything below follows from that.
| Layer | May do | May not do |
|---|---|---|
| Controller | Read the request, call one service, choose a representation | Business rules, EntityManager, DQL, flush() |
| Service | Every business rule, orchestration, flush() | Build queries, know that HTTP exists, return entities to the outside |
| Repository | DQL, QueryBuilder, SQL, persist(), remove() | Business rules, flush() |
| DTO | Carry data across a boundary, hold validation constraints | Depend on Doctrine, contain logic beyond normalisation |
| Entity | Mapped state, getters, setters, value constants | Invariants, publish()-style domain methods |
The contract holds by being followed; symfony-yoandev-quality ships a deptrac configuration
that verifies it mechanically, for the day a second developer or an unsupervised agent
makes review insufficient. If a class does not fit a layer, the class is in the wrong
place — do not widen the rule.
Entities are generated in maker format: private properties, getters, setters. That is a deliberate trade, not a law of nature, and its terms must be stated rather than discovered.
What it buys. One rule with no exceptions — every rule lives in a service — which is
easy to teach, easy to review, and checkable by deptrac. And zero friction with the
tooling: make:entity, the Form component, fixtures and EasyAdmin all expect setters and
a no-argument constructor, and all keep working untouched.
What it costs. A setter enforces nothing, so nothing technical stops
$book->setRating(9) outside BookRater. The guarantee rests on every write path going
through the service. That is a real weakness; it is bounded rather than eliminated:
symfony-yoandev-http).#[Assert] constraint on the entity, reading the same constants — that is the one
case where a constraint belongs on an entity (symfony-yoandev-doctrine).Rich entities — a constructor for the mandatory fields, rate() instead of
setRating() — would close that gap for local rules. They are rejected here because they
pay for it in every tool listed above, and because most rules outgrow the entity the day
they need a query. The reasoning is in references/rejections.md.
Therefore every rule lives in a service, and the entity is a typed row.
// src/Entity/Book.php — values, not rules. Constants are fine here: they are data.
public const MIN_RATING = 1;
public const MAX_RATING = 5;
public function setRating(?int $rating): static { $this->rating = $rating; return $this; }
// src/Service/BookRater.php — the rule, in the only place that can enforce it.
final readonly class BookRater
{
public function __construct(
private BookRepository $books,
private EntityManagerInterface $em,
private ObjectMapperInterface $mapper,
) {
}
public function rate(int $bookId, RateBookInput $input): BookView
{
if (null === $book = $this->books->find($bookId)) {
throw new BookNotFound($bookId);
}
if ($input->rating < Book::MIN_RATING || $input->rating > Book::MAX_RATING) {
throw new InvalidRating($input->rating);
}
$book->setRating($input->rating);
$this->em->flush();
return $this->mapper->map($book, BookView::class);
}
}
Do not write $book->rate(5) anywhere. If you find one, move the check into the service
that calls it. (ObjectMapperInterface is symfony/object-mapper — 7.3 experimental,
stable 7.4, absent on 6.4; without it, a static fromEntity() on the Output DTO. The
rule does not change, only the means — references/dtos.md.)
final readonly, constructor injection only. final because the extension point
is a decorator or a strategy, not inheritance; readonly because a service that
mutates itself is a service that behaves differently on the second request.
The exception this rule creates: a final concrete class cannot be decorated —
#[AsDecorator] on a service whose consumers typehint the concrete class fails
lint:container. So when a decorator is needed, extract an interface, alias the
implementation to it, decorate the interface id — rather than dropping final.
Worked example in references/dependency-injection.md.BookRater, ShelfReader, BookCreator —
not BookService, which is a folder pretending to be a class.flush(), once per use case. The repository does persist()
and remove(). One transaction boundary per business operation, visible in the method
that owns the operation. A flush() inside a repository method makes the boundary
invisible and turns two writes into two transactions.rate(int $bookId, …),
then $this->books->find($bookId) ?? throw new BookNotFound($bookId). The service
owns the "not found" because it must work the same from a controller, a command and a
message handler, and all three carry an id. The controller resolves the entity
(Book $book, the EntityValueResolver) in exactly one case: an attribute needs the
object before the action runs — #[IsGranted(…, subject: 'book')] on a voter. It
still passes $book->getId() to the service, and the service's find() costs no
query: the identity map already holds the row. Two paths to a 404, one rule for which.final — they are doubled in service unit
tests. Everything else is final.Failures that the caller cannot prevent get a named exception in src/Exception/,
carrying its own HTTP translation. That keeps the controller free of try/catch and
the mapping next to the meaning.
#[WithHttpStatus(Response::HTTP_NOT_FOUND)]
#[WithLogLevel(LogLevel::INFO)] // a missing book is not an incident
final class BookNotFound extends \DomainException
{
public function __construct(public readonly int $bookId)
{
parent::__construct(\sprintf('No book #%d on this shelf.', $bookId));
}
}
Both are Symfony\Component\HttpKernel\Attribute\*, since 6.3, and both are read
from parent classes, so a base App\Exception\NotFound propagates to its children.
Before 6.3, map the exception under framework.exceptions instead.
Two things that surprise people, both checked in ErrorListener:
#[WithHttpStatus] is ignored when the exception already implements
HttpExceptionInterface. Do not extend NotFoundHttpException and then decorate it.framework.exceptions in configuration wins over the attribute — over
#[WithLogLevel] as much as over #[WithHttpStatus]. An entry's log_level and
log_channel both beat what the attribute says, verified on 8.1. Use that entry to map
third-party exceptions you cannot annotate; use the attribute for your own, and do
not expect an attribute to win back a class the configuration already names.Leave the attribute off deliberately when the exception means "a bug reached here" —
InvalidRating above should be impossible, because the Input DTO's #[Assert\Range]
already rejected it at the HTTP edge. A 500 is the correct answer, and a silent 422
would hide the defect. Say so in a comment; the absence of an attribute otherwise reads
as an oversight.
When a service has done its work and something else must happen, it calls the collaborator:
$this->em->flush();
$this->notifier->notifyAuthor($review); // yes
Not $this->dispatcher->dispatch(new ReviewPublished($review)).
The reason is legibility under maintenance. With a direct call, the whole consequence of publishing a review is readable in one method, and the unit test asserts the notifier was called. With a domain event, answering "what happens when a review is published?" becomes a project-wide search, and the answer depends on listener priorities that appear nowhere near the code. The dispatcher buys decoupling that a single application does not need and charges for it on every future read.
#[AsEventListener] is for framework events only — kernel.exception,
kernel.response, Doctrine, Messenger, Security. One class per event, named after what
it does, with __invoke():
#[AsEventListener] // event inferred from the parameter type
final readonly class AddNoIndexHeaderOnStaging
{
public function __invoke(ResponseEvent $event): void { /* … */ }
}
Inference from the type declaration needs no event: argument; pass one only for events
identified by a string name. EventSubscriberInterface is rejected: more ceremony, and a
static method to keep in sync with the class.
Kernel listeners are a last resort. Reach first for the dedicated mechanism —
#[WithHttpStatus]/#[WithLogLevel] for errors, a ValueResolver to inject an argument,
#[Cache] for headers, a Voter for authorisation. A kernel listener runs on everything,
far from the code it affects, and debugs badly. Write one only when nothing else exists.
| Ask | Answer | Where |
|---|---|---|
| Does it differ per machine or per environment? | DSN, base URLs, API keys | env var |
| Is it application behaviour, identical everywhere? | retention days, feature flag | parameter prefixed app. |
| Does it almost never change? | page size, rating bounds | class constant |
public function __construct(
#[Autowire(param: 'app.shelf_page_size')] private int $pageSize,
#[Autowire(env: 'bool:FEATURE_IMPORT')] private bool $importEnabled,
) {
}
#[Autowire] accepts exactly one of value, service, expression, env or param —
passing two throws at compile time.
Secrets: .env.local in development, the platform's secret store in production, and
the Symfony secrets vault the moment neither covers the case. The suite deploys a
container image (symfony-yoandev-deployment), and every container platform already has a
secret store that injects environment variables. There, the vault would be a second
store whose own decryption key still has to come from the first; one store is enough.
The vault is not rejected on its merits — its decryption key is one environment
variable, no harder to deploy than a DSN, and it is the only mechanism that lets a
secret be versioned and reviewed. It is the answer as soon as there is no platform store
(a bare server, an rsync deploy), or as soon as secrets must travel through the
repository. What is rejected is .env.local on a production host, and two mechanisms
on one project.
Constructor injection covers almost everything. When it does not, the attributes are in
Symfony\Component\DependencyInjection\Attribute\ — every one of them verified against
vendor/, with the version each landed in, in references/dependency-injection.md. That
file also covers Strategy (#[AutowireLocator] + #[AsTaggedItem]), Decorator
(#[AsDecorator] + #[AutowireDecorated]), Factory, and the two attributes this standard
deliberately does not use: #[Required] and #[SubscribedService].
Voters are authorisation, not architecture. They exist, they are the right tool for
object-level permissions, and they belong to symfony-yoandev-security. Do not put a permission
check in a service "because rules live in services".
| Symptom | What it actually means |
|---|---|
$em->flush() in a controller | The use case has no service yet |
A QueryBuilder in a service | The query needs a named repository method |
A service typehinting Request | The controller should have passed an Input DTO |
A template calling book.ratings|length | An entity escaped the service layer |
An if on a role inside a service | That is a Voter (symfony-yoandev-security) |
| A service constructor with eight arguments | Two use cases wearing one class name |
new SomeService(...) inside a service | A collaborator that should be injected |
| File | When to read it |
|---|---|
references/dtos.md | Designing Input / Read / Output DTOs, or normalising a projection |
references/dependency-injection.md | Injecting anything constructor autowiring cannot resolve; Strategy, Decorator, Factory |
references/rejections.md | Someone proposes an alternative — check whether it was already rejected, and why |
What SKILL.md sets; muted values are Claude Code's defaults
/symfony-yoandev-skills:symfony-yoandev-architecturefrom Symfony Skills
npx claudepluginhub yoanbernabeu/symfony-yoandev-skills --plugin symfony-yoandev-skills16 skills in this plugin
/symfony-yoandev-asyncMove work out of the request with Messenger, send email asynchronously with Mailer and TemplatedEmail, and run recurring jobs with Scheduler — Doctrine transport, retry and failure policy, prioritised queues, idempotent handlers, and workers under supervisor or systemd. Use this skill whenever someone asks to send an email, do something in the background, queue a job, run something every night or every five minutes, replace a crontab, speed up a slow import or a slow signup, process a big CSV, notify someone after an action, set up Messenger or a message handler or a worker, configure retries, deal with a message stuck in the failure queue, or says "my emails never arrive", "the import times out", "this page is slow because it sends mail", or "how do I schedule this"./symfony-yoandev-consoleWrite Symfony console commands as thin translation layers over a service: one class per command family with method-level #[AsCommand] on Symfony 8.1, invokable commands with #[Argument] and #[Option] before that, SymfonyStyle output, dry run by default with --force on anything destructive, mandatory locking on long or scheduled commands, clean shutdown on SIGTERM, and a smoke test with runCommand or CommandTester. Use this skill whenever someone asks to add a command, write a script, make something runnable from the terminal, build an import or an export or a batch job, clean up or purge or delete old data, backfill a column, send a digest, "run this once", "I need a CLI for this", "make:command", says a command times out or eats all the memory or deleted more than it should, or asks how to test a command. For scheduling that command to run every night, and for Messenger workers, use symfony-yoandev-async instead./symfony-yoandev-deploymentShip a Symfony application to production as a Docker image running FrankenPHP: the build and release sequence and what each step is for, migrations run automatically at deploy, production php.ini and OPcache preloading, environment variables and secrets on the server, Messenger workers under systemd or supervisor, post-deployment checks and rollback. Use this skill whenever someone says deploy this, put it in production, ship it, set up the server, prepare a Dockerfile or a production image, write a deploy script, run migrations on the server, restart the workers, configure OPcache or preloading, set environment variables in production, roll back a release — and equally when they describe a symptom: it works locally but not on the server, the site broke after deploying, my CSS is missing in production, production is slow, the workers are running old code, I get a 500 with no error message, the new code is not being picked up. Also use it before a first deployment, when nobody knows how the app is deployed./symfony-yoandev-doctrineThe persistence layer of a Symfony project: entities in maker format with attribute mapping, auto-increment integer ids, \DateTimeImmutable and native enums; relations and the collection traps they open; repositories as the single place where DQL, QueryBuilder and raw SQL are allowed; SELECT NEW projections into Read DTOs; who calls flush(); and migrations that are generated then read and corrected by hand. Use this skill whenever someone asks to create or change an entity, add a field or a column, store or persist something, link two things together, add a relation or a foreign key, write or fix a repository method, write a query, sort or filter a list from the database, count rows, make a query return less data, create or run a migration, rename a column without losing data, or asks "why is my query wrong", "why is this null", "why does saving not work", "the schema is out of sync", "I lost data after deploying"./symfony-yoandev-frontendBuild and debug the front end of a Symfony application with AssetMapper, Twig, Stimulus, Turbo and Symfony UX — no bundler, no Node build step. Use this skill whenever someone asks to make a page interactive, add a filter or a live search, add an autocomplete or a dropdown, add a JavaScript library, style a page or set up Tailwind, add an icon, build a reusable Twig component or a Live Component, add Turbo Drive, Frames or Streams, write a Stimulus controller, organise templates or translation keys, or set a form theme. Also use it for the failures: "my JavaScript does not run", "my JS stopped working after clicking a link", "the CSS did not update", "the page is slow to load", "the assets 404 in production", "the images are served by PHP", "importmap:audit is failing" — and whenever the question is *where* a piece of behaviour belongs, between a Stimulus controller, a Live Component and a Twig Component./symfony-yoandev-httpWrite the HTTP layer of a Symfony application: controllers, routes, request input mapping, JSON and HTML responses, forms and Twig templates. One controller class per resource, a class-level route prefix, input DTOs hydrated by #[MapRequestPayload] or a FormType, output through #[Serialize] for JSON and #[Template] for HTML, RFC 7807 Problem Details for API errors, and an items + meta envelope for lists. Use this skill whenever someone asks to add a page, add a route or an endpoint, expose something as JSON, build an API, make a CRUD, build or wire up a form, map an uploaded file off the request, read a query parameter, paginate a list, return a 404 or a 422, protect an action with a CSRF token, render or restructure a Twig template, or says their endpoint returns a 500, their form never validates, their JSON has the wrong shape, or their route matches the wrong action. For what then happens to that uploaded file — where it is stored, how it is served back, who may read it — use `symfony-yoandev-storage` instead.Other plugins' skills for the same job