-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add information on how to get a user in a provider #1751
base: 3.1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -125,6 +125,40 @@ use App\State\BlogPostProvider; | |||||
class BlogPost {} | ||||||
``` | ||||||
|
||||||
## Getting the user in a provider | ||||||
|
||||||
Receiving a user in your provider to allow for filtering of entities can be achieved by adding the `Security` class in your constructor; | ||||||
|
||||||
```php | ||||||
<?php | ||||||
|
||||||
namespace App\State; | ||||||
|
||||||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||||||
use ApiPlatform\Metadata\Operation; | ||||||
use ApiPlatform\State\ProviderInterface; | ||||||
use App\Repository\BlogPostRepository; | ||||||
use Symfony\Bundle\SecurityBundle\Security; | ||||||
|
||||||
class BlogPostProvider implements ProviderInterface | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
public function __construct( | ||||||
private readonly BlogPostRepository $blogPostRepository, | ||||||
private readonly Security $security | ||||||
) { | ||||||
} | ||||||
|
||||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||||||
{ | ||||||
if ($operation instanceof CollectionOperationInterface) { | ||||||
return $this->blogPostRepository->findAllForUser($this->security->getToken()->getUser()); | ||||||
} | ||||||
|
||||||
return $this->blogPostRepository->find($uriVariables['id']); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe should this be removed as this doesn't look related? Also, you should set the |
||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
## Hooking into the Built-In State Provider | ||||||
|
||||||
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). | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.