Li Li Li Li - 3 months ago 16x
PHP Question

Check url before redirect symfony2

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

I wan't to redirect to the
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.


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');