Understanding Operations as PHP Classes
Understanding Operations as PHP Classes
~14 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026
The SIX endpoints from chapter 10 don't come from magic – they come from SIX concrete PHP classes in the ApiPlatform\Metadata namespace. A bare #[ApiResource] simply activates ALL of them with their DEFAULT settings.
Listing operations explicitly
#[ApiResource(
operations: [
new GetCollection(),
new Get(),
new Post(),
new Put(),
new Patch(),
new Delete(),
]
)]
#[ORM\Entity(repositoryClass: ProjectRepository::class)]
class Project
{
// ... unchanged
}EXACTLY the same behavior as the empty #[ApiResource] from chapter 9 – but now VISIBLE, and therefore individually adjustable ON EACH operation. The classes live in ApiPlatform\Metadata and MUST be imported.
Item operations vs. collection operations
API Platform distinguishes TWO operation families, recognizable by the class name:
| Operation type | Behavior |
|---|---|
GetCollection | Acts on the COLLECTION – path WITHOUT {id}, e.g. GET /api/projects |
Get, Put, Patch, Delete | Act on a SINGLE item – path WITH {id}, e.g. GET /api/projects/1 |
Post | SPECIAL CASE: creates a NEW item, but the path has NO {id} (that only comes into existence THROUGH the operation) |
Configuring operations individually
The REAL benefit of the explicit list: EACH operation accepts ITS OWN parameters – for example a custom description for Swagger UI that appears ONLY on this one operation.
#[ApiResource(
operations: [
new GetCollection(),
new Get(),
new Post(
description: 'Creates a new project. The name must be unique.'
),
new Put(),
new Patch(),
new Delete(),
]
)]
This description appears DIRECTLY in the Swagger UI from chapter 6, ONLY on the POST operation – GET /api/projects does NOT show it, since it's bound to a DIFFERENT operation.
Tipp: This class list is the KEY to everything else in this block: chapter 12 REMOVES individual classes from the list (read-only resources), chapter 13 adds uriTemplate per class, and chapter 61 LATER adds ENTIRELY NEW, hand-written operation classes.