Li Li Li Li - 5 months ago 32
PHP Question

Check url before redirect symfony2

if ($u = $this->generateUrl('_'.$specific.'_thanks'))
return $this->redirect($u);
else
return $this->redirect($this->generateUrl('_thanks'));


I wan't to redirect to the
_specific_thanks
url when it exist. So How to check if a url exist?

When I did that, I had this error:


Route "_specific_thanks" does not exist.

Answer

I don't think there's a direct way to check if a route exists. But you can look for route existence through the router service.

$router = $this->container->get('router');

You can then get a route collection and call get() for a given route, which returns null if it doesn't exist.

$router->getRouteCollection()->get('_'. $specific. '_thanks');