/symfony-skills:api-platform-resourcesfrom symfony-skills
Guides exposing data through API Platform 4: defining #[ApiResource], DTO resources, state providers/processors, serialization groups, filters, and pagination.
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.
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.
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);
}
}
// ✅ 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.
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 = [];
}
security:new Post(security: "is_granted('ROLE_USER')"),
new Delete(security: "is_granted('ORDER_DELETE', object)"), // voter on the object
#[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.
order:read / order:write groups stable.problem-details-rfc9457). Throw domain exceptions mapped to status codes.#[ApiResource] on a Doctrine entity — prefer a DTO resource with provider/processor for non-trivial APIs.#[Groups] — every property gets exposed, including sensitive ones.findAll() from a collection provider — keep pagination on.@ApiResource) — use PHP 8 attributes and the v4 metadata classes (ApiPlatform\Metadata\*).What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skills17 skills in this plugin
/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.
/doctrine-migrationsOther plugins' skills for the same job