Skip to content

project rulesBackend and APIsphpsymfony

Symfony Rules

Project rules for Symfony apps: autowired services, thin controllers, Doctrine repositories, Form types, Messenger handlers and PHPUnit tests.

npx workspacetemplates add symfony-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file22 lines · 1.5 KBFile
# Project rules: Symfony

## Structure
- Keep controllers thin: map the request, call one service, return a response. Use attribute routing and `#[MapRequestPayload]` or `#[MapQueryString]` for input DTOs.
- Business logic lives in services under `src/Service` or per feature. Inject dependencies through the constructor; never fetch from the container or use static helpers.
- Use `declare(strict_types=1)`, typed properties and readonly value objects.

## Doctrine
- Queries belong in repository classes, never in controllers. Select only what you need and fetch-join relations that will be read, to avoid N+1.
- Change the schema only with migrations from `doctrine:migrations:diff`; read the SQL before committing. Do not call `flush()` inside loops.
- Do not serialize entities directly. Return DTOs or use serialization groups.

## Forms and validation
- Put rules on DTOs or entities with Validator constraints. Use Form types for HTML forms, and never trust data that skipped validation.

## Messenger and config
- Send slow work (mail, exports, webhooks) through Messenger messages with idempotent handlers. Configure retry and a failure transport.
- Configuration goes in `.env` defaults plus secrets in the vault. Autowire parameters with `#[Autowire]`.

## Tests and checks
- Use PHPUnit with `WebTestCase` for endpoints and `KernelTestCase` for services. Use fixtures, not production data.
- Before finishing run `composer validate`, `vendor/bin/phpstan analyse`, `vendor/bin/php-cs-fixer fix --dry-run` and `bin/phpunit`.

What it does

Symfony gives you many ways to reach the same result, and an agent will pick a different one each time unless told. These rules choose one: attribute routing, constructor autowiring, thin controllers and logic in services.

They also cover Doctrine, forms and async work with Messenger, and finish with the checks the agent must run before reporting a change as done.

Example

you › Add an endpoint that lets a customer export their invoices as CSV

agent › The agent adds a controller action that dispatches an ExportInvoices message, a handler that streams rows from a repository query, a DTO for the filters, and a WebTestCase plus a handler test.

Questions

Does this fit Symfony without Doctrine?
Yes. Ignore the Doctrine section; the structure, Messenger and test rules do not depend on an ORM.
Why ban the container inside services?
Hidden dependencies make a class hard to test and hide what it needs. Constructor injection shows them at a glance.
  • ASP.NET Core Web API Rules

    Project rulesdotnet

    Free
  • Django Rules

    Project rulespython

    Free
  • Flask Rules

    Project rulespython

    Free
  • Go Service Rules

    Project rulesgo

    Free