Skip to content

Commit 1c29700

Browse files
committed
Explain how to check if a route exists
1 parent ac45677 commit 1c29700

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

components/routing.rst

+22
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)