Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how to check if a route exists #9845

Merged
merged 1 commit into from
May 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down