Api-Platform:在 SF 4 中使用 Yaml 配置而不是注释 [英] Api-Platform: Using Yaml config instead of annotations in SF 4

查看:25
本文介绍了Api-Platform:在 SF 4 中使用 Yaml 配置而不是注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Api-Platform 中使用 YAML 而不是注释.

I'd like to use YAML instead of annotations in Api-Platform.

我没有使用 Api-Platform 发行版,而是将 api-pack 添加到我现有的 Symfony Flex 项目中(composer req api).

Instead of using the Api-Platform distribution, I have added the api-pack into my existing Symfony Flex project (composer req api).

文档说 YAML 文件应该在 /config/api_platform/resources.yaml 中,但我的实体没有被发现.

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?

谢谢,本

推荐答案

你唯一需要做的就是添加以下配置:

The only thing you need to do is to add the following configuration:

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/config/api_platform/resources']

我在里面使用了一个名为 resources 的子文件夹来将配置拆分为多个文件.下面是一个配置示例:

I use a subfolder named resources inside to split the configuration into many files. Here is an example of configuration:

文章.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(在配置中有更多内容)

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)

这篇关于Api-Platform:在 SF 4 中使用 Yaml 配置而不是注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆