简体   繁体   中英

Symfony 5.4 debug toolbar not loading because of my dynamic route

Im trying to create a cms with Symfony. I want users to be able to create their own pages. I got this all set up but what did not work for me was loading pages with an url that contains a / .

This is my route that does not allow a / :

#[Route('/{slug}', name: 'dynamic_page', defaults: ["slug" => null], methods: ['GET'])]
public function showStoredPage(Page $page)
{
    return $this->render('blocks/base.html.twig');
}

If I add requirements: ["slug" => ".+"] to my route, I am able to go to any/route/that/exists with as many slashes as I wish. Which is what I want.

But after adding this I figured that my debug toolbar does not load anymore. The debug bar shows loading... in all pages I have. Also, when looking at the text the symfony built-in server shows in cmd, it keeps looping a request. If I wait long enough the server even crashes because it can never load the toolbar...

How can I make my toolbar load but also keep my route dynamic the way I want it? I'm using Symfony 5.4 with PHP 8+.

It appears this can be solved very easily. I just had to add priority: -1 to my route attribute. Now it looks like this:

#[Route('/{slug}', name: 'dynamic_page', defaults: ["slug" => null], requirements: ["slug" => ".+"], methods: ['GET'], priority: -1)]

Also with the command php bin/console debug:router you can now see the route is on the bottom of the table and the toolbar will load first.

For more control over what the route does, and does not match, without having to rely on the priority setting, you can use a more precise regex. You can use a negative lookahead to define what not to match. All the paths you want to avoid start with '_' so, you could use '(?._).+' but that will prevent you from matching anything starting with '_' . To only exclude the defined routes use '(?._(error|wdt|profiler)).+'

#[Route('/{slug}', name: 'dynamic_page', defaults: ["slug" => null], requirements: ["slug" => "(?!_(error|wdt|profiler)).+"], methods: ['GET']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM