Symfony4 Api平台:在SF 4中使用Yaml配置而不是注释

Symfony4 Api平台:在SF 4中使用Yaml配置而不是注释,symfony4,api-platform.com,Symfony4,Api Platform.com,我想在Api平台中使用YAML而不是注释 我没有使用Api平台发行版,而是将Api包添加到现有的Symfony Flex项目中(composer req Api) 文档中说YAML文件应该发生在/config/api_platform/resources.YAML中,但是没有发现我的实体 我应该在其他地方配置吗 谢谢,, Ben我遇到了与您相同的问题,我使用中描述的服务装饰来避免问题 config/services.yaml src/Swagger/SwaggerDecorator.php 您只

我想在Api平台中使用YAML而不是注释

我没有使用Api平台发行版,而是将Api包添加到现有的Symfony Flex项目中(
composer req Api

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

我应该在其他地方配置吗

谢谢,,
Ben

我遇到了与您相同的问题,我使用中描述的服务装饰来避免问题

config/services.yaml

src/Swagger/SwaggerDecorator.php


您只需添加以下配置:

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/config/api_platform/resources']
我使用名为
resources
inside的子文件夹将配置拆分为多个文件。以下是一个配置示例:

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/config/api_platform/resources']
第1条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(配置中包含更多内容)


完美答案。谢谢你,奈克!嗨,当使用yaml安装的注释时,有没有一种解决方案可以实现“自动完成”?谢谢如果我需要使用这个装饰方法创建多个路由,我应该怎么做?我看到的所有示例都只创建一条路由。@JoaoVictorSouza复制并调整
$customDefinition=…
$docs['path'][…]
。您有Post操作的示例吗?显示正文请求值?如果您需要覆盖单个响应,您可以通过添加
swagger\u context
openapi\u context
(选择您使用的)并用swagger的方案描述它来完成:
# /config/api_platform/resources/article.yaml
App\Domain\Article:
    attributes:
        normalization_context:
            groups: ['article_read']

    collectionOperations: []

    itemOperations:
        get:
            method: 'GET'
        put:
            method: 'PUT'
# 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)