From 1c297009ab137460b09311c41f1bbbe7c88324a3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 28 May 2018 16:33:03 +0200 Subject: [PATCH] Explain how to check if a route exists --- components/routing.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/components/routing.rst b/components/routing.rst index b1690cce6bf..21c706e267c 100644 --- a/components/routing.rst +++ b/components/routing.rst @@ -229,6 +229,28 @@ a certain route:: of the current :class:`Symfony\\Component\\Routing\\RequestContext` does not match the requirement. +Check if a Route Exists +~~~~~~~~~~~~~~~~~~~~~~~ + +In highly dynamic applications, it may be necessary to check whether a route +exists before using it to generate a URL. In those cases, don't use the +:method:`Symfony\\Component\\Routing\\Router::getRouteCollection` method because +that regenerates the routing cache and slows down the application. + +Instead, try to generate the URL and catch the +:class:`Symfony\\Component\\Routing\\Exception\\RouteNotFoundException` thrown +when the route doesn't exist:: + + use Symfony\Component\Routing\Exception\RouteNotFoundException; + + // ... + + try { + $url = $generator->generate($dynamicRouteName, $parameters); + } catch (RouteNotFoundException $e) { + // the route is not defined... + } + Load Routes from a File ~~~~~~~~~~~~~~~~~~~~~~~