Using getRouteCollection() at run time is not the right solution. To perform this method, you need to reinstall the cache. This means that routing caching will be rebuilt on every request, which will make your application much slower than necessary.
If you want to check if a route exists or not, use the try ... catch construct:
use Symfony\Component\Routing\Exception\RouteNotFoundException; try { dump($router->generate('some_route')); } catch (RouteNotFoundException $e) { dump('Oh noes, route "some_route" doesn't exists!'); }
Wouter j
source share