Skip to main content

Check if a route name exists

Posted on January 29, 2020


$route_provider = \Drupal::service('router.route_provider');
$route_provider->getRouteByName('abc.xyz')

If 'abc.xyz' exists it will return a \Symfony\Component\Routing\Route object. Otherwise it will throw an exception.

If you don't want an exception use this instead:

$route_provider = \Drupal::service('router.route_provider');
$exists = count($route_provider->getRoutesByNames(['abc.xyz'])) === 1;