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

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

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

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

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

我应该在其他地方配置一些东西吗?

谢谢, 本

我遇到了和你一样的问题,我通过使用 documentation 中描述的服务装饰避免了这个问题。

config/services.yaml

# Customize Swagger documentation
'App\Swagger\SwaggerDecorator':
    decorates: 'api_platform.swagger.normalizer.documentation'
    arguments: [ '@App\Swagger\SwaggerDecorator.inner' ]
    autoconfigure: false

src/Swagger/SwaggerDecorator.php

<?php

namespace App\Swagger;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
 * @link https://api-platform.com/docs/core/swagger/
 */
final class SwaggerDecorator implements NormalizerInterface
{
    private $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        $this->decorated = $decorated;
    }

    public function normalize($object, $format = null, array $context = [])
    {
        $docs = $this->decorated->normalize($object, $format, $context);

        $customDefinition = [
            'name' => 'id',
            'description' => 'ID of user',
            'in' => 'path',
            'required' => 'true',
        ];

        $docs['paths']['/api/ben/stack_overflow/{id}']['post'] = [
            'summary' => 'SO example',
            'parameters' => [
                $customDefinition,
            ],
            'responses' => [
                '200' => [
                    'description' => 'OK',
                ],
                '400' => [
                    'description' => 'Error',
                ],
            ],
        ];
    }

    public function supportsNormalization($data, $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }
}

语法与 Swagger 相同,但写成 PHP 数组而不是 JSON。

它将生成以下 Swagger 文档:

(我还不知道如何更改 default 标题)

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

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

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

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(配置内容较多)

# 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)