Search code examples
phpsymfonysymfony4symfony-routing

How to add prefix to some controllers using annotations?


I have at the moment two controllers.

  • GitlabAuthController
  • UserController

I need to add the path prefix api to UserController only.

Before I was trying following at annotations.yaml file:

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix: api

But this adds the prefix to all my controllers.

Is there any way I can add the exception for the GitlabAuthController?


Solution

  • Just create different directories/namespaces for the different types of controllers.

    Then you can do:

    controllers:
        resource: ../../src/Controller/
        type: annotation
    
    api_controllers:
        resource: ../../src/Controller/Api
        type: annotation
        prefix: api
    

    Routes defined on the Api namespace would get the /api/ prefix, while the other routes would remain unaffected.

    You can check the generated routes are fine by executing bin/console debug:router.