Search code examples
phpsymfonymodel-view-controllerroutessymfony-3.2

Symfony 3 - Route excluding beginning of specific path (url)


I'm creating an application with Symfony 3.2.9, and I would to do a Admin panel to manage application. Application works like CMS, so is creating new pages with URL, like domain.com/pagename1 and also domain.com/pagename1/subpagelevel2 ect. Problem is when I want to create URL for Admin panel, and URL should looks like: domain.com/admin, but also admin panel need some sub pages, like domain.com/admin/manage or domain.com/admin/manage/edit/1 ect.

I created DefaultController with routing like :

/**
 * @Route("/", name="homepage")
 */

and AdminController with routing like :

/**
 * @Route("/admin", name="admin")
 */

Problem is that when I want to dynamically create new sub page of application I need to create routing like:

/**
 * @Route("/{page}")
 */

But this overwrite my Admin panel sub pages (eg. domain.com/admin/manage).

Is it way, to exclude or overwrite path from default DefaultController by AdminController? I want to have possibility to create all URL-s from DefaultController excepts paths beginning like domain.com/admin ... and so on.


Solution

  • From documention in https://symfony.com/doc/current/routing.html you can use the requirements parameter to specify a more strict match

    I guess something like this whould work:

    DefaultController:

    /**
     * @Route("/", name="homepage")
     */
    

    AdminController:

    /**
     * @Route("/admin", name="admin")
     */
    

    Other Controller:

    /**
     * we exclude page=admin from this controller
     * @Route("/{page}", requirements={"page": "^(?!admin).+"}))
     */