diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index 534670f8de..0077724ba3 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -209,6 +209,24 @@ public function getRouteByName($name) { return reset($routes); } + /** + * Determines if a route exists by name. + * + * @param string $name + * The name of the route to check. + * + * @return bool + * Whether a route with that route name exists. + */ + public function routeExists($name) : bool { + + if (empty($name) || !is_string($name)) { + return FALSE; + } + + return (count($this->getRoutesByNames([$name])) === 1); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Routing/RouteProviderInterface.php b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php index 5bfc659eef..becdc0d754 100644 --- a/core/lib/Drupal/Core/Routing/RouteProviderInterface.php +++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php @@ -77,6 +77,17 @@ public function getRouteByName($name); */ public function getRoutesByNames($names); + /** + * Determines if a route exists by name. + * + * @param string $name + * The name of the route to check. + * + * @return bool + * Whether a route with that route name exists. + */ + public function routeExists($name); + /** * Get all routes which match a certain pattern. * diff --git a/core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php b/core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php index 350e400a68..77afa2fc15 100644 --- a/core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php +++ b/core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php @@ -639,7 +639,7 @@ public function testRouteCaching() { } /** - * Tests RouteProvider::getRouteByName() & RouteProvider::getRoutesByNames(). + * Tests RouteProvider::getRouteByName(), RouteProvider::getRoutesByNames(), and RouteProvider::routeExists(). */ public function testRouteByName() { $connection = Database::getConnection(); @@ -651,6 +651,14 @@ public function testRouteByName() { $dumper->addRoutes($this->fixtures->sampleRouteCollection()); $dumper->dump(); + $emptyRouteName = ''; + $this->assertFalse($provider->routeExists($emptyRouteName)); + $emptyRouteName = NULL; + $this->assertFalse($provider->routeExists($emptyRouteName)); + $this->assertFalse($provider->routeExists('route_z')); + $this->assertTrue($provider->routeExists('route_a')); + $this->assertTrue($provider->routeExists('route_b')); + $route = $provider->getRouteByName('route_a'); $this->assertEquals('/path/one', $route->getPath(), 'The right route pattern was found.'); $this->assertEquals(['GET'], $route->getMethods(), 'The right route method was found.'); diff --git a/core/tests/Drupal/KernelTests/RouteProvider.php b/core/tests/Drupal/KernelTests/RouteProvider.php index f075dbae0d..53985da611 100644 --- a/core/tests/Drupal/KernelTests/RouteProvider.php +++ b/core/tests/Drupal/KernelTests/RouteProvider.php @@ -42,6 +42,14 @@ public function getRouteByName($name) { return $this->lazyLoadItself()->getRouteByName($name); } + /** + * {@inheritdoc} + */ + public function routeExists($name) { + return $this->lazyLoadItself()->routeExists($name); + } + /** * {@inheritdoc} */