Skip to content

Commit 1905113

Browse files
committed
minor symfony#9845 Explain how to check if a route exists (javiereguiluz)
This PR was merged into the 2.8 branch. Discussion ---------- Explain how to check if a route exists This fixes symfony#6710. Commits ------- 1c29700 Explain how to check if a route exists
2 parents 72181cc + 1c29700 commit 1905113

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

components/routing.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,28 @@ a certain route::
229229
of the current :class:`Symfony\\Component\\Routing\\RequestContext` does
230230
not match the requirement.
231231

232+
Check if a Route Exists
233+
~~~~~~~~~~~~~~~~~~~~~~~
234+
235+
In highly dynamic applications, it may be necessary to check whether a route
236+
exists before using it to generate a URL. In those cases, don't use the
237+
:method:`Symfony\\Component\\Routing\\Router::getRouteCollection` method because
238+
that regenerates the routing cache and slows down the application.
239+
240+
Instead, try to generate the URL and catch the
241+
:class:`Symfony\\Component\\Routing\\Exception\\RouteNotFoundException` thrown
242+
when the route doesn't exist::
243+
244+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
245+
246+
// ...
247+
248+
try {
249+
$url = $generator->generate($dynamicRouteName, $parameters);
250+
} catch (RouteNotFoundException $e) {
251+
// the route is not defined...
252+
}
253+
232254
Load Routes from a File
233255
~~~~~~~~~~~~~~~~~~~~~~~
234256

0 commit comments

Comments
 (0)