如何从Nelmio Docs隐藏Api平台文件

时间:2019-03-19 10:19:50

标签: symfony symfony4 api-platform.com api-doc nelmioapidocbundle

我希望有人能帮助我在Nelmio中使用Api-platorm。

我使用Api平台和Nelmio。我需要对Nelmio隐藏Api平台文档。

我需要3条路线:

/internal -> API-Platform Docs
/external -> NELMIO-Docs
/admin -> NELMIO-Docs

我的Nelmio配置:

# config/packages/nelmio_api_doc.yaml
nelmio_api_doc:
    documentation:
        info:
            title: ...
            description: ...
            version: 0.2.0
    areas: # to filter documented areas
        default:
            path_patterns: [ ^/external ]
        external:
            path_patterns: [ ^/external ]
        admin:
            path_patterns: [ ^/admin ]

我的Nelmio配置(路线):

# config/routes/nelmio_api_doc.yaml
app.swagger:
    path: /{area}/json
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger, area: default }

app.swagger_ui:
    path: /{area}
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger_ui, area: default }

我的API平台配置:

# config/routes/api_platform.yaml
api_platform:
    resource: .
    type: api_platform
    prefix: /internal/

但是,如果我转到http://localhost/externalhttp://localhost/admin,则不仅会看到总是需要的路由,而且还会看到来自API平台的路由:

enter image description here

2 个答案:

答案 0 :(得分:1)

我知道这个问题现在已经很老了,但是我面临着同样的情况,我发现了一种可能对某些人有所帮助的解决方法,所以我将其发布。

API平台使您可以装饰Swagger,以便可以自定义最终文档输出。当我不要求它时,我利用了它摆脱了整个api平台文档。

<?php

namespace App\Swagger;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class SwaggerDecorator implements NormalizerInterface
{
    private $decorated;
    private $requestStack;

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

    public function normalize($object, $format = null, array $context = [])
    {
        if ('/internal/docs' !== $this->requestStack->getCurrentRequest()->getPathInfo()) {
            // request is not for internal docs (maybe it is for external or admin one) so get rid of api platform docs
            return null;
        }

        $docs = $this->decorated->normalize($object, $format, $context);

        // here you can customize documentation

        return $docs;
    }

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

我希望这对某人有帮助,编码愉快!

答案 1 :(得分:0)

在您的nelmio配置yaml文件上,使用正则表达式排除文档。例如,要排除/external/doc,您应该:

 external:
            path_patterns: [ ^/external(?!/doc$) ]