Search code examples
symfonysymfony-routing

Symfony2 Routing: import all controllers' annotations BUT exclude one (duplicated routes in NelmioApiDoc)


The problem

When we define in routing.yml:

my_controllers:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /

and:

my_api:
    resource:    'MyBundle\Controller\ApiController'
    type:        rest
    prefix:      /api
    name_prefix: my_api_
    options:
        expose: true

We get duplicated routes in NelmioApiDoc:

enter image description here

The question

How we should do this so routes from ApiController won't be duplicated?

  • exclude ApiController from "wildcard" resource import?
  • import all other controllers individually?
  • other solution?

Solution

  • You get duplicated entries in Nelmio ApiDoc because there are actually duplicated routes in Symfony. Execute this command to see every route in Symfony, including those of your API (that will appear both with the /api prefix and without it):

    php app/console router:debug
    

    As Yann suggests, I think the best approach is to create a separate directory for your API Controllers (something like @MyBundle/ControllerRest/) and place them there. Then, don't forget to update the resource route under my_api section in your routing.yml and the namespaces in your API Controllers.

    This way you get rid of the duplicated routes and thus ApiDoc will only show one route per call, instead of two.