Skip to content

docs: improve state provider and state processor for laravel #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions core/state-processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,47 @@ final class UserProcessor implements ProcessorInterface
}
```

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:

```php
<?php
// app/Providers/AppServiceProvider.php

namespace App\Providers;

use App\State\UserProcessor;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Laravel\Eloquent\State\PersistProcessor;
use ApiPlatform\Laravel\Eloquent\State\RemoveProcessor;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->app->tag(UserProcessor::class, ProcessorInterface::class);

$this->app->singleton(UserProcessor::class, function (Application $app) {
return new UserProcessor(
$app->make(PersistProcessor::class),
$app->make(RemoveProcessor::class),
);
});
}
}
```

Finally, configure that you want to use this processor on the User resource:

```php
Expand Down
38 changes: 37 additions & 1 deletion core/state-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,43 @@ final class BookRepresentationProvider implements ProviderInterface
}
```

And configure that you want to use this provider on the Book resource:
And we bind the [ItemProvider](https://github.com/api-platform/core/blob/main/src/Laravel/Eloquent/State/ItemProvider.php) in our Service Provider

```php
<?php
// app/Providers/AppServiceProvider.php

namespace App\Providers;

use App\State\BookRepresentationProvider;
use ApiPlatform\Laravel\Eloquent\State\ItemProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->app->singleton(BookRepresentationProvider::class, function (Application $app) {
return new BookRepresentationProvider(
$app->make(ItemProvider::class),
);
});
}
}
```

Finally, configure that you want to use this provider on the Book resource:

```php
<?php
Expand Down