api-platform-mutators
Skill2 starsChanged 6 months agoMITView on GitHub
Use API Platform 4.3's new `#[AsResourceMutator(resourceClass: …)]` and `#[AsOperationMutator(operationName: …)]` to mutate resource/operation metadata at **build time** — zero runtime cost. Implement `ResourceMutatorInterface::__invoke(ApiResource $resource): ApiResource` to e.g. prefix every operation's `routePrefix`, or `OperationMutatorInterface::__invoke(Operation $operation): Operation` to e.g. inject an extra serialization group. Includes the decision matrix between Mutator (build-time, cached, applies always) and Context Builder (runtime, per-request, role-/user-/header-dependent — see `api-platform-serialization`). Trigger on "apply the same change to every operation of a resource", "global API prefix injected from code", or "build-time metadata modification".
In this repository52 other files it gives its agents.
- api-platform-dto-resourcesSkill
- api-platform-errorsSkill
- api-platform-file-uploadSkill
- api-platform-filtersSkill
- api-platform-identifiersSkill
- api-platform-mcpSkill
- api-platform-openapiSkill
- api-platform-paginationSkill
- api-platform-performanceSkill
- api-platform-resilienceSkill
- api-platform-resourcesSkill
- api-platform-securitySkill
- api-platform-serializationSkill
- api-platform-state-processorsSkill
- api-platform-state-providersSkill
- api-platform-testsSkill
- api-platform-upgradeSkill
- api-platform-userSkill
- api-platform-versioningSkill
- bootstrap-checkSkill
- brainstormingSkill
- config-env-parametersSkill
- controller-cleanupSkill
- cqrs-and-handlersSkill
- daily-workflowSkill
- doctrine-batch-processingSkill
- doctrine-fetch-modesSkill
- doctrine-fixtures-foundrySkill
- doctrine-migrationsSkill
- doctrine-relationsSkill
- doctrine-transactionsSkill
- effective-contextSkill
- executing-plansSkill
- functional-testsSkill
- interfaces-and-autowiringSkill
- makefile-disciplineSkill
- messenger-retry-failuresSkill
- ports-and-adaptersSkill
- quality-checksSkill
- rate-limitingSkill
- runner-selectionSkill
- strategy-patternSkill
- symfony-cacheSkill
- symfony-messengerSkill
- symfony-schedulerSkill
- symfony-votersSkill
- tdd-with-pestSkill
- tdd-with-phpunitSkill
- test-doubles-mockingSkill
- using-symfony-superpowersSkill
- value-objects-and-dtosSkill
- writing-plansSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
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
- Very specific: line 28
OperationandApiResourceare immutable: Always use withOperations(), withNormalizationContext(), withRoutePrefix(), etc.
≈ “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.3 — Resource & Operation Mutators (NEW 4.3)
Use when
- You want to apply a uniform metadata change to a resource (prefix routes, inject a group) without touching every operation by hand.
- The change is decidable at build time (no runtime dependency on the request).
- You're tempted to write a Context Builder for something that does not depend on the request — Mutator is cheaper.
Default workflow
- Decide: build-time (Mutator) or runtime (Context Builder)?
- For a resource-wide change, implement
ResourceMutatorInterfaceand annotate with#[AsResourceMutator(resourceClass: Book::class)]. - For a single-operation change, implement
OperationMutatorInterfaceand annotate with#[AsOperationMutator(operationName: '_api_Book_get_collection')]. - Return the modified
ApiResource/Operation(these objects are immutable — usewith*()mutators).
Guardrails
- Mutator = build time, no access to the request / user / locale. If you need any of those, use a Context Builder.
OperationandApiResourceare immutable. Always usewithOperations(),withNormalizationContext(),withRoutePrefix(), etc.- Operation names are stable (
_api_<ShortName>_<operation>by convention). Pin them in a constant if you reference them in multiple mutators.
Progressive disclosure
SKILL.mdcovers posture and decision tree.reference.mdcarries the fullAsResourceMutatorandAsOperationMutatorrecipes + the Context Builder vs Mutator comparison table.
Output contract
- Mutators in
src/ApiPlatform/Mutator/with explicit#[AsResourceMutator]/#[AsOperationMutator]annotations. - Build-time metadata changes verified via
php bin/console debug:router | grep apiandphp bin/console api:openapi:export --yaml. - A short comment in each mutator explaining what it changes and why.
References
reference.md
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.

