Reputation: 103
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.
Upvotes: 3
Views: 3077
Reputation: 7010
Since Symfony 5.1, you can define route priority :
/**
* @Route("/admin", name="admin", priority=10)
*/
/**
* @Route("/{slug}", name="pages", priority=99)
*/
Upvotes: 0
Reputation: 1265
Cleanest on your use case:
Why not simply create a separate Bundle for Admin and put a prefix in AdminBundle routes?
Depend on routing orders and/or REGEX in routes is not recommended just to avoid a Bundle Creation. This is for what Bundles have been thought.
app/config/routing.yml
admin:
resource: "@AdminBundle/Controller/"
type: annotation
prefix: /admin
Then, all controllers/routes under AdminBundle will work under /admin prefix.
For example, IndexController/DefaultController/WhatEverController with this route inside AdminBundle:
/**
* @Route("/")
*/
public function indexAction()
{
//My code
}
Will match "/admin" instead of "/"
And:
/**
* @Route("/{page}")
*/
public function indexAction()
{
//My code
}
Will match "/admin/{page}" instead of "/{page}"
Upvotes: 0
Reputation: 35149
Routes are searched in the order they are listed - so put the most generic at the end of the list, and it will find and use /admin before /{page}
For example, one of my last routes at the bottom of app/conf/routing.yml is
# http://symfony.com/doc/current/routing/redirect_trailing_slash.html
remove_trailing_slash:
path: /{url}
defaults:
_controller: AppBundle:Default:removeTrailingSlash
requirements:
url: .*/$
methods: [GET]
Upvotes: 1
Reputation: 921
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).+"}))
*/
Upvotes: 5