agentleFS
fatonhfatonh/symfony-skills/skills/api-platform-resources/SKILL.md

api-platform-resources

Skill35 starsChanged 5 months agoMITView on GitHub

Use 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.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If you *do* expose entities (small internal apps), always scope fields with serialization groups and disable the operations you don't want.

    line 51
  2. Distinctive:

    Use the built-in Pagination in collection providers.

    line 91
  3. Distinctive:

    Put Symfony Validator constraints on the input DTO.

    line 95
  4. Very specific:

    Don't hand-build error responses — API Platform emits RFC 9457 problem+json (see problem-details-rfc9457).

    line 139

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

API Platform 4 Resources

Prefer DTO resources over exposing entities

Exposing a Doctrine entity directly as #[ApiResource] couples your public API to your schema and leaks persistence concerns. For anything beyond a trivial CRUD prototype, expose a DTO resource backed by a state provider (read) and state processor (write).

// ✅ GOOD — a resource DTO, decoupled from the entity
#[ApiResource(
    shortName: 'Order',
    operations: [
        new Get(provider: OrderItemProvider::class),
        new GetCollection(provider: OrderCollectionProvider::class),
        new Post(processor: CreateOrderProcessor::class, input: CreateOrderInput::class),
    ],
    normalizationContext: ['groups' => ['order:read']],
)]
final class OrderResource
{
    public function __construct(
        #[ApiProperty(identifier: true)]
        #[Groups(['order:read'])]
        public string $id,

        #[Groups(['order:read'])]
        public string $status,

        /** @var LineItemResource[] */
        #[Groups(['order:read'])]
        public array $items = [],
    ) {}
}
// ❌ BAD — Doctrine entity as the API resource: schema is now your public contract
#[ApiResource]
#[ORM\Entity]
class Order { /* every column auto-exposed, lazy associations serialized, N+1 */ }

If you do expose entities (small internal apps), always scope fields with serialization groups and disable the operations you don't want.

State processors (write side)

A processor receives the input DTO and performs the use case — usually by delegating to an application service / Messenger bus.

// ✅ GOOD
/** @implements ProcessorInterface<CreateOrderInput, OrderResource> */
final class CreateOrderProcessor implements ProcessorInterface
{
    public function __construct(private readonly OrderService $orders) {}

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): OrderResource
    {
        $order = $this->orders->createOrder($data);   // $data is the validated CreateOrderInput

        return OrderResource::fromEntity($order);
    }
}

State providers (read side)

// ✅ GOOD — provider returns the read model, not the entity
/** @implements ProviderInterface<OrderResource> */
final class OrderItemProvider implements ProviderInterface
{
    public function __construct(private readonly OrderRepository $orders) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?OrderResource
    {
        $order = $this->orders->find(Uuid::fromString($uriVariables['id']));

        return $order ? OrderResource::fromEntity($order) : null;
    }
}

Use the built-in Pagination in collection providers; don't return unbounded arrays.

Validation

Put Symfony Validator constraints on the input DTO. API Platform validates automatically before the processor runs and returns RFC 9457 problem+json on failure.

final class CreateOrderInput
{
    #[Assert\NotBlank]
    #[Assert\Email]
    public string $customerEmail;

    /** @var CreateLineInput[] */
    #[Assert\Valid]
    #[Assert\Count(min: 1)]
    public array $items = [];
}

Operations & security

  • Declare operations explicitly; don't accept the default full CRUD set unless you mean it.
  • Authorize per-operation with security:
new Post(security: "is_granted('ROLE_USER')"),
new Delete(security: "is_granted('ORDER_DELETE', object)"),  // voter on the object

Filters & pagination

#[ApiResource(
    operations: [new GetCollection()],
    paginationItemsPerPage: 20,
    paginationMaximumItemsPerPage: 100,
)]
#[ApiFilter(SearchFilter::class, properties: ['status' => 'exact', 'customerEmail' => 'partial'])]
#[ApiFilter(OrderFilter::class, properties: ['createdAt' => 'DESC'])]
final class OrderResource { /* ... */ }

For large datasets, see doctrine-query-optimization — switch collection providers to keyset pagination.

Versioning & errors

  • Version through the path prefix or a vendor media type; keep order:read / order:write groups stable.
  • Don't hand-build error responses — API Platform emits RFC 9457 problem+json (see problem-details-rfc9457). Throw domain exceptions mapped to status codes.

Gotchas

  • Agent slaps #[ApiResource] on a Doctrine entity — prefer a DTO resource with provider/processor for non-trivial APIs.
  • Agent leaves all CRUD operations enabled by default — declare only the operations you intend to expose.
  • Agent forgets serialization #[Groups] — every property gets exposed, including sensitive ones.
  • Agent puts business logic in the processor — delegate to an application service; the processor just adapts.
  • Agent returns the entity from a provider — return the resource DTO.
  • Agent disables pagination or returns findAll() from a collection provider — keep pagination on.
  • Agent hand-rolls error JSON — let API Platform produce problem+json; throw mapped exceptions.
  • Agent uses API Platform 2/3 annotations (@ApiResource) — use PHP 8 attributes and the v4 metadata classes (ApiPlatform\Metadata\*).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.