Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Api-Platform: Using Yaml config instead of annotations in SF 4

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

like image 864
Ben Avatar asked Apr 12 '18 05:04

Ben


1 Answers

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)
like image 71
Nek Avatar answered Nov 05 '22 07:11

Nek