Skip to content

Commit 24814ce

Browse files
authored
feat: add built in state provider decoration doc
1 parent 6e294ff commit 24814ce

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

core/state-providers.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ In the following examples we will create custom state providers for an entity cl
1717
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using
1818
`#[ApiProperty(identifier: true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
1919

20-
## State Provider
20+
## Creating a Custom State Provider
2121

2222
If the [Symfony MakerBundle](https://symfony.com/doc/current/bundles/SymfonyMakerBundle) is installed in your project,
2323
you can use the following command to generate a custom state provider easily:
@@ -124,3 +124,65 @@ use App\State\BlogPostProvider;
124124
#[ApiResource(provider: BlogPostProvider::class)]
125125
class BlogPost {}
126126
```
127+
128+
## Hooking into the Built-In State Provider
129+
130+
If you want to execute custom business logic before or after retrieving data, this can be achieved by [decorating](https://symfony.com/doc/current/service_container/service_decoration.html) the built-in state providers or using [composition](https://en.wikipedia.org/wiki/Object_composition).
131+
132+
The next example uses a [DTO](https://api-platform.com/docs/core/dto/#using-data-transfer-objects-dtos) to change the presentation for data originally retrieved by the default state provider.
133+
134+
```php
135+
<?php
136+
137+
namespace App\State;
138+
139+
use App\Dto\AnotherRepresentation;
140+
use App\Model\Book;
141+
use ApiPlatform\Metadata\Operation;
142+
use ApiPlatform\State\ProviderInterface;
143+
144+
final class BookRepresentationProvider implements ProviderInterface
145+
{
146+
public function __construct(private ProviderInterface $itemProvider)
147+
{
148+
}
149+
150+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
151+
{
152+
$book = $this->itemProvider->provide($operation, $uriVariables, $context);
153+
154+
return new AnotherRepresentation(
155+
// Add DTO constructor params here.
156+
// $book->getTitle(),
157+
);
158+
}
159+
}
160+
```
161+
162+
Even with service autowiring and autoconfiguration enabled, you must still configure the decoration:
163+
164+
```yaml
165+
# api/config/services.yaml
166+
services:
167+
# ...
168+
App\State\BookRepresentationProvider:
169+
bind:
170+
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
171+
# Uncomment only if autoconfiguration is disabled
172+
#tags: [ 'api_platform.state_provider' ]
173+
```
174+
175+
And configure that you want to use this provider on the Book resource:
176+
177+
```php
178+
<?php
179+
180+
namespace App\Entity;
181+
182+
use ApiPlatform\Metadata\Get;
183+
use App\Dto\AnotherRepresentation;
184+
use App\State\BookRepresentationProvider;
185+
186+
#[Get(output: AnotherRepresentation::class, provider: BookRepresentationProvider::class)]
187+
class Book {}
188+
```

0 commit comments

Comments
 (0)