Symfony2 wildcard routing

1.1k views Asked by At

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)
{
}
1

There are 1 answers

2
Geoffroy On BEST ANSWER

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.