简体   繁体   中英

Symfony2 wildcard routing

I'm trying to create a special route that handles confirmation of a certain action. For instance, if I'm trying to access a route /admin/user/1/delete , I want to go to a different route first to show a special html page that confirms they want to complete an action (ie. confirm/admin/user/1/delete) and on the page there's a confirm button that goes to the route and a go back button that redirects to the referrer.

In the example below, is there a way to allow {route} to be anything and pass that into a twig page?

/**
* @Route("/confirm/{route}", name="confirm_dialog")
* @param type $route
*/
public function confirmAction($route)
{
}

There is a cookbook covering this specific use case: http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html

By default, the Symfony Routing component requires that the parameters match the following regex path: "[^/]+". This means that all characters are allowed except "/".
You must explicitly allow "/" to be part of your parameter by specifying a more permissive regex path: ".+"

/**
* @Route("/confirm/{route}", name="confirm_dialog", requirements={"route"=".+"})
* @param type $route
*/
public function confirmAction($route)
{
    // ...
}

But I would not recommend the pattern you are trying to use. I would recommend to have the same route " /admin/user/1/delete " using GET to display a confirmation form and using POST to perform the deletion. If you don't want to repeat the code for asking confirmation, you can always extract that specific code.

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