如何在“/api”文档 api-platform 上添加我的自定义错误

wal*_*ili 1 error-handling symfony api-platform.com

我正在使用 Symfony 4 和 Api-Platform 开发一个应用程序。

xxxException根据这些文档创建了一个自定义错误。我在手术后使用过它,效果很好。

现在我想在访问some-url/api.

怎样做,才会出现下图这样的情况呢?

在此输入图像描述

Sim*_*lev 6

您可以按照此处的SwaggerDecorator说明创建一个。

然后在您的方法App\Swagger\SwaggerDecorator中的类中normalize,您可以像这样更改文档(这里是添加文档和对自定义登录操作的响应的示例):

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

    $documentation['paths']['/login'] = [ // "/login" is the path of the operation
        'post' => [ // "post" is the http request method
            'responses' => [
                '200' => [ // 200 is the http response code
                    'description' => 'Successful log in.',
                ],
                '401' => [ // 401 another http response code
                    'description' => 'Bad credentials.',
                ],
            ],
        ],
    ];

    return $documentation;
}
Run Code Online (Sandbox Code Playgroud)

注意:文档变量只是一个数组或一个对象,ArrayAccess您可以将其转储并根据需要进行编辑。