You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/extending.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,7 @@ The following tables summarizes which extension point to use depending on what y
20
20
|[Messenger Handlers](../symfony/messenger.md)| create 100% custom, RPC, async, service-oriented endpoints (should be used in place of custom controllers because the messenger integration is compatible with both REST and GraphQL, while custom controllers only work with REST) |
21
21
|[DTOs](dto.md)| use a specific class to represent the input or output data structure related to an operation |
22
22
|[Kernel Events](events.md)| customize the HTTP request or response (REST only, other extension points must be preferred when possible) |
23
+
|[Operations and Resources](operations.md)| use mutators to dynamically alter metadata (works for third party API endpoints) |
Copy file name to clipboardExpand all lines: core/operations.md
+73Lines changed: 73 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -589,3 +589,76 @@ class Weather
589
589
```
590
590
591
591
That's it!
592
+
593
+
## Customize Operation and Resource Metadata
594
+
595
+
Metadata mutators allow a dynamic control over resources and operations, by programmatically altering metadata before they are exposed as endpoints. Providing a way to modify, add or remove operations, adjust serialization groups or pagination settings.
596
+
597
+
It also makes it possible to customize built-in endpoints from a third-party API, such as Sylius.
598
+
599
+
### Resource Mutator
600
+
601
+
Use the resource mutator to modify the entire resource metadata by adding the attribute and target resource class as argument:
602
+
603
+
```php
604
+
<?php
605
+
// src/Entity/Book.php
606
+
namespace App;
607
+
608
+
use ApiPlatform\Metadata\ApiResource;
609
+
use ApiPlatform\Metadata\AsResourceMutator;
610
+
use ApiPlatform\Metadata\ResourceMutatorInterface;
611
+
use App\Entity\User;
612
+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
613
+
614
+
#[AsResourceMutator(resourceClass: Book::class)]
615
+
final readonly class UserApiDomainMutator implements ResourceMutatorInterface
616
+
{
617
+
// .env
618
+
// API_PREFIX="/library"
619
+
public function __construct(#[Autowire(env: 'API_PREFIX')] private string $apiDomain) {
620
+
}
621
+
622
+
public function __invoke(ApiResource $resource): ApiResource
0 commit comments