Search code examples
symfonyroutessymfony4

Symfony set base route for all controllers that extend a certain controller


Let's assume in Symfony you have a BackendController.

Then all backend controllers follow e.g. ProjectsController extends BackendController.

Is there a way to set Symfony up so that all controllers that extend BackendController route to somesite.com/auth

Such that

class ProjectsController extends BackendController
{

    /**
     * @Route("projects");
     */
    public function index()  {

route projects resolves to auth/projects without having to explicitly state it @Route("auth/projects");?

Please be gentle; I'm extremely new to Symfony.


Solution

  • The class level route definitions are not inherited meaning that this isn't possible. The next best thing you can do is to define that base route separately on class level for all controllers inheriting the base class:

    /**                                                                             
     * @Route("/auth")
     */
    class ProjectsController extends BackendController
    {
        /**
         * @Route("/projects");
         */
        public function index()  {
            ...
        }
    
        ...
    }