Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1905113

Browse filesBrowse files
committed
minor #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 #6710. Commits ------- 1c29700 Explain how to check if a route exists
2 parents 72181cc + 1c29700 commit 1905113
Copy full SHA for 1905113

File tree

Expand file treeCollapse file tree

1 file changed

+22
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+22
-0
lines changed

‎components/routing.rst

Copy file name to clipboardExpand all lines: components/routing.rst
+22Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.