im building a website using symfony 4. The website's pages are dynamically created in an admin section.
How can i create an exception or requirements that the rout rendering the custom pages should only be used for custom page and will not affect routs for login and register? here is my rout
/**
* @Route("/{page}", name="subpages", requirements={"page"="\d "})
*/
public function subpages(Request $request): Response
{
$page = $request->get('page');
$content = $this->getDoctrine()->getRepository(Pages::class)->find($page);
return $this->render('public_pages/subpage.html.twig', [
'controller_name' => 'home',
'content' => $content
]);
}
this case, i want to only use that rout if the {page} is not /login or /register
Thank you in advance
CodePudding user response:
The order of your controller functions matter, you should put /login
and /register
before your subpage
function.
However, sometimes it might not be possible due to other functions being in different controllers files with different names etc making ordering difficult..
You can use Regex in the requirements. So in your case you could do this:
@Route("/{page}", name="subpages", requirements={"page"="^(?!\blogin\b|\bregister\b). "})
This will match any route except login
or register
. You can add more into the regex with a word boundary, eg \bcontact\b
.
This might not be the best approach if you have lots of routes however as it can be hard to keep track. Instead you could also consider having the route like "/pages/{page}"
Symfony 5.1 supports priority which makes this situation easier to deal with using the priority
parameter in the annotation.