Skip to content

Commit a70e3bf

Browse files
docs: improve state provider and state processor for laravel (#2162)
Co-authored-by: Antoine Bluchet <[email protected]>
1 parent 846e383 commit a70e3bf

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

core/state-processors.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,41 @@ final class UserProcessor implements ProcessorInterface
264264
}
265265
```
266266

267+
Next, we bind the [PersistProcessor](https://github.com/api-platform/core/blob/main/src/Laravel/Eloquent/State/PersistProcessor.php) and [RemoveProcessor](https://github.com/api-platform/core/blob/main/src/Laravel/Eloquent/State/RemoveProcessor.php) in our Service Provider:
268+
269+
```php
270+
<?php
271+
// app/Providers/AppServiceProvider.php
272+
273+
namespace App\Providers;
274+
275+
use App\State\UserProcessor;
276+
use ApiPlatform\State\ProcessorInterface;
277+
use ApiPlatform\Laravel\Eloquent\State\PersistProcessor;
278+
use ApiPlatform\Laravel\Eloquent\State\RemoveProcessor;
279+
use Illuminate\Support\ServiceProvider;
280+
use Illuminate\Contracts\Foundation\Application;
281+
class AppServiceProvider extends ServiceProvider
282+
{
283+
/**
284+
* Register any application services.
285+
*/
286+
public function register(): void
287+
{
288+
$this->app->tag(UserProcessor::class, ProcessorInterface::class);
289+
290+
$this->app->singleton(UserProcessor::class, function (Application $app) {
291+
return new UserProcessor(
292+
$app->make(PersistProcessor::class),
293+
$app->make(RemoveProcessor::class),
294+
);
295+
});
296+
}
297+
298+
// ...
299+
}
300+
```
301+
267302
Finally, configure that you want to use this processor on the User resource:
268303

269304
```php

core/state-providers.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,37 @@ final class BookRepresentationProvider implements ProviderInterface
345345
}
346346
```
347347

348-
And configure that you want to use this provider on the Book resource:
348+
And we bind the [ItemProvider](https://github.com/api-platform/core/blob/main/src/Laravel/Eloquent/State/ItemProvider.php) in our Service Provider
349+
350+
```php
351+
<?php
352+
// app/Providers/AppServiceProvider.php
353+
354+
namespace App\Providers;
355+
356+
use App\State\BookRepresentationProvider;
357+
use ApiPlatform\Laravel\Eloquent\State\ItemProvider;
358+
use Illuminate\Support\ServiceProvider;
359+
use Illuminate\Contracts\Foundation\Application;
360+
class AppServiceProvider extends ServiceProvider
361+
{
362+
/**
363+
* Register any application services.
364+
*/
365+
public function register(): void
366+
{
367+
$this->app->singleton(BookRepresentationProvider::class, function (Application $app) {
368+
return new BookRepresentationProvider(
369+
$app->make(ItemProvider::class),
370+
);
371+
});
372+
}
373+
374+
//...
375+
}
376+
```
377+
378+
Finally, configure that you want to use this provider on the Book resource:
349379

350380
```php
351381
<?php

0 commit comments

Comments
 (0)