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
- gerard-labs/superpowers-api-platformtheir own version, first seen Oct 13, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- gerard-labs/superpowers-api-platform★ 2same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository16 other files it gives its agents.
- bundle-organizationSkill
- console-commandsSkill
- doctrine-migrationsSkill
- doctrine-ormSkill
- doctrine-query-optimizationSkill
- domain-driven-designSkill
- dto-and-validationSkill
- hexagonal-architectureSkill
- jwt-authenticationSkill
- layered-architectureSkill
- messenger-asyncSkill
- problem-details-rfc9457Skill
- rest-controller-conventionsSkill
- security-authenticationSkill
- testing-pyramidSkill
- transactional-patternsSkill
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
- Distinctive: line 51
If you *do* expose entities (small internal apps), always scope fields with serialization groups and disable the operations you don't want.
- Distinctive: line 91
Use the built-in Pagination in collection providers.
- Distinctive: line 95
Put Symfony Validator constraints on the input DTO.
- Very specific: line 139
Don't hand-build error responses — API Platform emits RFC 9457 problem+json (see problem-details-rfc9457).
≈ “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.
Files
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 objectFilters & 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:writegroups 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\*).
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
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.

