I'd like to use YAML instead of annotations in Api-Platform.
Instead of using the Api-Platform distribution, I have added the api-pack into my existing Symfony Flex project (composer req api
).
The documentation says the YAML file should take place in /config/api_platform/resources.yaml
but my entities aren't discovered.
Should I configure something somewhere else?
Thank you, Ben
The only thing you need to do is to add the following configuration:
api_platform:
mapping:
paths: ['%kernel.project_dir%/config/api_platform/resources']
I use a subfolder named resources
inside to split the configuration into many files. Here is an example of configuration:
article.yaml
# /config/api_platform/resources/article.yaml
App\Domain\Article:
attributes:
normalization_context:
groups: ['article_read']
collectionOperations: []
itemOperations:
get:
method: 'GET'
put:
method: 'PUT'
user.yaml (with more content in config)
# This file is inside /config/api_platform/resources/user.yaml
App\Domain\User:
attributes:
normalization_context:
groups: ['user_read']
denormalization_context:
api_allow_update: true
groups: ['user_write', 'user_avatar_write']
filters:
- 'App\Application\ApiPlatform\Filters\DeletedFilter'
collectionOperations:
get:
method: 'GET'
access_control: is_granted('VIEW', object)
normalization_context: {'groups': ['user_read_collection']}
post:
method: 'POST'
access_control: is_granted('CREATE', object)
normalization_context:
groups: ['user_post']
itemOperations:
get:
method: 'GET'
access_control: is_granted('VIEW', object)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With