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
[WIP] Add Laravel support to state providers documentation
This commit enhances the state providers documentation by including support for Laravel alongside Symfony. It details how to create custom state providers, configure them, and integrate with Laravel-specific ORM like Eloquent and MongoDB, ensuring parity with Symfony's state provider features.
To retrieve data exposed by the API, API Platform uses classes called **state providers**. A state provider using [Doctrine
4
-
ORM](https://www.doctrine-project.org/projects/orm.html) to retrieve data from a database, a state provider using
3
+
To retrieve data exposed by the API, API Platform uses classes called **state providers**.
4
+
5
+
With the Symfony variant, a state provider using [Doctrine
6
+
ORM](https://www.doctrine-project.org/projects/orm.html) is ready to retrieve data from a database and a state provider using
5
7
[Doctrine MongoDB ODM](https://www.doctrine-project.org/projects/mongodb-odm.html) to retrieve data from a document
6
-
database, and a state provider using [Elasticsearch-PHP](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html)
7
-
to retrieve data from an Elasticsearch cluster are included with the library. The first one is enabled by default. These
8
-
state providers natively support paged collections and filters. They can be used as-is and are perfectly suited to common uses.
8
+
database.
9
+
10
+
With the Laravel variant, a state provider using [Eloquent ORM](https://laravel.com/docs/eloquent) to retrieve data from a relational database and a state provider using [Laravel MongoDB](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/) to retrieve data from a document database.
11
+
12
+
The ORM providers are enabled by default, based on your framework variant (Eloquent or Doctrine will be set up).
13
+
14
+
Also, both Symfony and Laravel variant come with a state provider to retrieve data from an Elasticsearch cluster using the library [Elasticsearch-PHP](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html).
15
+
16
+
These state providers natively support paged collections and filters. They can be used as-is and are perfectly suited to common uses.
9
17
10
18
However, you sometimes want to retrieve data from other sources such as another persistence layer or a webservice.
11
19
Custom state providers can be used to do so. A project can include as many state providers as needed. The first able to
12
20
retrieve data for a given resource will be used.
13
21
14
22
To do so you need to implement the `ApiPlatform\State\ProviderInterface`.
15
23
16
-
In the following examples we will create custom state providers for an entity class called `App\Entity\BlogPost`.
17
-
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using
24
+
In the following examples we will create custom state providers for Symfony entities and Laravel models:
25
+
- For Symfony we will create an entity class called `App\Entity\BlogPost`.
26
+
- For Laravel, we will create a model class called `App\Models\BlogPost`.
27
+
28
+
Note, that if your entity is not Doctrine-related or Eloquent-related, you need to flag the identifier property by using
18
29
`#[ApiProperty(identifier: true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
19
30
20
31
## Creating a Custom State Provider
21
32
33
+
### Custom State Provider with Symfony
34
+
22
35
If the [Symfony MakerBundle](https://symfony.com/doc/current/bundles/SymfonyMakerBundle) is installed in your project,
23
36
you can use the following command to generate a custom state provider easily:
24
37
@@ -114,7 +127,7 @@ final class BlogPostProvider implements ProviderInterface
114
127
}
115
128
```
116
129
117
-
We then need to configure this same provider on the BlogPost `GetCollection` operation, or for every operations via the `ApiResource` attribute:
130
+
We then need to configure this same provider on the BlogPost `GetCollection` operation, or for every operation via the `ApiResource` attribute:
118
131
119
132
```php
120
133
<?php
@@ -129,11 +142,124 @@ use App\State\BlogPostProvider;
129
142
class BlogPost {}
130
143
```
131
144
145
+
#### Custom State Provider with Laravel
146
+
147
+
Using [Laravel Artisan Console](https://laravel.com/docs/artisan), you can generate a custom state provider easily with the following command:
148
+
149
+
```console
150
+
php artisan make:state-provider
151
+
```
152
+
153
+
Let's start with a State Provider for the URI: `/blog_posts/{id}`.
154
+
155
+
First, your `BlogPostProvider` has to implement the
final class BlogPostProvider implements ProviderInterface
172
+
{
173
+
private const DATA = [
174
+
'ab' => new BlogPost('ab'),
175
+
'cd' => new BlogPost('cd'),
176
+
];
177
+
178
+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
179
+
{
180
+
return self::DATA[$uriVariables['id']] ?? null;
181
+
}
182
+
}
183
+
```
184
+
185
+
For the example, we store the list of our blog posts in an associative array (the `BlogPostProvider::DATA` constant).
186
+
187
+
As this operation expects a `BlogPost`, the `provide` methods return the instance of the `BlogPost` corresponding to the ID passed in the URL. If the ID doesn't exist in the associative array, `provide()` returns `null`. API Platform will automatically generate a 404 response if the provider returns `null`.
188
+
189
+
The `$uriVariables` parameter contains an array with the values of the URI variables.
190
+
191
+
To use this provider we need to configure the provider on the operation:
192
+
193
+
```php
194
+
<?php
195
+
// api/src/Models/BlogPost.php
196
+
197
+
namespace App\Models;
198
+
199
+
use ApiPlatform\Metadata\Get;
200
+
use App\State\BlogPostProvider;
201
+
202
+
#[Get(provider: BlogPostProvider::class)]
203
+
class BlogPost {}
204
+
```
205
+
206
+
Now let's say that we also want to handle the `/blog_posts` URI which returns a collection. We can change the Provider into
207
+
supporting a wider range of operations. Then we can provide a collection of blog posts when the operation is a `CollectionOperationInterface`:
208
+
209
+
```php
210
+
<?php
211
+
// api/src/State/BlogPostProvider.php
212
+
213
+
namespace App\State;
214
+
215
+
use App\Models\BlogPost;
216
+
use ApiPlatform\Metadata\Operation;
217
+
use ApiPlatform\State\ProviderInterface;
218
+
use ApiPlatform\Metadata\CollectionOperationInterface;
final class BlogPostProvider implements ProviderInterface
224
+
{
225
+
private const DATA = [
226
+
'ab' => new BlogPost('ab'),
227
+
'cd' => new BlogPost('cd'),
228
+
];
229
+
230
+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|BlogPost|null
231
+
{
232
+
if ($operation instanceof CollectionOperationInterface) {
233
+
return self::DATA;
234
+
}
235
+
236
+
return self::DATA[$uriVariables['id']] ?? null;
237
+
}
238
+
}
239
+
```
240
+
241
+
We then need to configure this same provider on the BlogPost `GetCollection` operation, or for every operation via the `ApiResource` attribute:
242
+
243
+
```php
244
+
<?php
245
+
// api/src/Models/BlogPost.php
246
+
247
+
namespace App\Models;
248
+
249
+
use ApiPlatform\Metadata\ApiResource;
250
+
use App\State\BlogPostProvider;
251
+
252
+
#[ApiResource(provider: BlogPostProvider::class)]
253
+
class BlogPost {}
254
+
```
255
+
132
256
## Hooking into the Built-In State Provider
133
257
134
258
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).
135
259
136
-
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.
260
+
The next examples (one for Symfony and one for Laravel) 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.
261
+
262
+
### Symfony State Provider mechanism
137
263
138
264
```php
139
265
<?php
@@ -142,7 +268,7 @@ The next example uses a [DTO](https://api-platform.com/docs/core/dto/#using-data
142
268
namespace App\State;
143
269
144
270
use App\Dto\AnotherRepresentation;
145
-
use App\Model\Book;
271
+
use App\Entity\Book;
146
272
use ApiPlatform\Metadata\Operation;
147
273
use ApiPlatform\State\ProviderInterface;
148
274
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -187,7 +313,86 @@ use App\State\BookRepresentationProvider;
187
313
class Book {}
188
314
```
189
315
190
-
## Registering Services Without Autowiring
316
+
#### Laravel State Provider mechanism
317
+
318
+
First, don't forget to tag the service with the `ProviderInterface`
0 commit comments