How to Install Swagger on Symfony 4?

4.5k Views Asked by At

I started to install like this:
https://symfony.com/doc/4.x/bundles/NelmioApiDocBundle/index.html
Step 1.

`composer require nelmio/api-doc-bundle` - Thats OK

Step 2.

    class AppKernel extends Kernel
    {
        public function registerBundles()
        {
                $bundles = [
                    new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
                ];
        }
    }

But I have no AppKernel extends Kernel. I **just have Kernel** with
`$contents = require $this->getProjectDir().'/config/bundles.php';`
So I added into **/config/bundles.php**
Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true],

Step 3.
I added into config/routes.yaml

# config/routes.yaml
app.swagger_ui:
    path: /api/doc
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger_ui }

Step 4.
I created config/packages/nelmio_api_doc.yaml with

nelmio_api_doc:
    areas:
        path_patterns: # an array of regexps
            - ^/api(?!/doc$)
        host_patterns:
            - ^api\.

After that I should see Swagger's page at mysite/api/doc
But i see only white page with link 'NelmioApiDocBundle' at github
What I did wrong?

2

There are 2 best solutions below

0
On

You need to specify areas in your nelmio_api_doc

nelmio_api_doc:
    documentation:
        info:
            title: My App
            description: This is an awesome app!
            version: 1.0.0
    areas: # to filter documented areas
        path_patterns:
            - ^/api(?!/doc$) # Accepts routes under /api except /api/doc
            - ^/secured/project(?!/doc$) # Accepts routes under /api except /api/doc

0
On

I ran:

bin/console assets:install --symlink

and it's now working.