RouteNotFoundException example

    $this->routeProvider
      ->method('getRouteByName')
      ->with('route_1')
      ->willReturn($this->testRoutes['route_1']);
    $lazyRouteCollection = new LazyRouteCollection($this->routeProvider);
    $this->assertEquals($lazyRouteCollection->get('route_1')$this->testRoutes['route_1']);

    // Miss.     $this->routeProvider
      ->method('getRouteByName')
      ->with('does_not_exist')
      ->will($this->throwException(new RouteNotFoundException()));

    $lazyRouteCollectionFail = new LazyRouteCollection($this->routeProvider);
    $this->assertNull($lazyRouteCollectionFail->get('does_not_exist'));
  }

}
$this->assertEquals(FALSE, $this->accessManager->checkNamedRoute('test_route_1', []$this->account));
    $this->assertEquals(AccessResult::forbidden()$this->accessManager->checkNamedRoute('test_route_1', []$this->account, TRUE));
  }

  /** * Tests checkNamedRoute given an invalid/non existing route name. */
  public function testCheckNamedRouteWithNonExistingRoute() {
    $this->routeProvider->expects($this->any())
      ->method('getRouteByName')
      ->will($this->throwException(new RouteNotFoundException()));

    $this->setupAccessChecker();

    $this->assertEquals(FALSE, $this->accessManager->checkNamedRoute('test_route_1', []$this->account), 'A non existing route lead to access.');
    $this->assertEquals(AccessResult::forbidden()->addCacheTags(['config:core.extension'])$this->accessManager->checkNamedRoute('test_route_1', []$this->account, TRUE), 'A non existing route lead to access.');
  }

  /** * Tests that an access checker throws an exception for not allowed values. * * @dataProvider providerCheckException */
if (null !== $locale) {
            do {
                if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
                    $name .= '.'.$locale;
                    break;
                }
            } while (false !== $locale = strstr($locale, '_', true));
        }

        if (!isset($this->compiledRoutes[$name])) {
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
        }

        [$variables$defaults$requirements$tokens$hostTokens$requiredSchemes$deprecations] = $this->compiledRoutes[$name] + [6 => []];

        foreach ($deprecations as $deprecation) {
            trigger_deprecation($deprecation['package']$deprecation['version']$deprecation['message']);
        }

        if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
            if (!\in_array('_locale', $variables, true)) {
                unset($parameters['_locale']);
            }
if (null !== $locale) {
            do {
                if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
                    $name .= '.'.$locale;
                    break;
                }
            } while (false !== $locale = strstr($locale, '_', true));
        }

        if (!isset($this->compiledRoutes[$name])) {
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
        }

        [$variables$defaults$requirements$tokens$hostTokens$requiredSchemes$deprecations] = $this->compiledRoutes[$name] + [6 => []];

        foreach ($deprecations as $deprecation) {
            trigger_deprecation($deprecation['package']$deprecation['version']$deprecation['message']);
        }

        if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
            if (!\in_array('_locale', $variables, true)) {
                unset($parameters['_locale']);
            }
if ($alias->isDeprecated()) {
                    $deprecations[] = $deprecation = $alias->getDeprecation($currentId);
                    trigger_deprecation($deprecation['package']$deprecation['version']$deprecation['message']);
                }

                $visited[] = $currentId;
                $currentId = $alias->getId();
            }

            if (null === $target = $routes->get($currentId)) {
                throw new RouteNotFoundException(sprintf('Target route "%s" for alias "%s" does not exist.', $currentId$name));
            }

            $compiledTarget = $target->compile();

            $compiledAliases[$name] = [
                $compiledTarget->getVariables(),
                $target->getDefaults(),
                $target->getRequirements(),
                $compiledTarget->getTokens(),
                $compiledTarget->getHostTokens(),
                $target->getSchemes(),
                

  protected function getRoute($name) {
    if ($name === '<front>') {
      return new Route('/');
    }
    elseif ($name === '<current>') {
      return new Route($this->requestStack->getCurrentRequest()->getPathInfo());
    }
    elseif ($name === '<none>') {
      return new Route('');
    }
    throw new RouteNotFoundException();
  }

  /** * {@inheritdoc} */
  protected function processRoute($name, Route $route, array &$parameters, BubbleableMetadata $bubbleable_metadata = NULL) {
  }

  /** * {@inheritdoc} */
  
$url->setUrlGenerator($url_generator);

    $data[] = [$url, TRUE];

    // Non-existent routed URL.     $url = Url::fromRoute('example.not_existing_route');

    $url_generator = $this->createMock('Drupal\Core\Routing\UrlGeneratorInterface');
    $url_generator->expects($this->any())
      ->method('generateFromRoute')
      ->with('example.not_existing_route', [][])
      ->willThrowException(new RouteNotFoundException());
    $url->setUrlGenerator($url_generator);

    $data[] = [$url, FALSE];

    foreach ($data as &$single_data) {
      $link = $this->createMock('Drupal\link\LinkItemInterface');
      $link->expects($this->any())
        ->method('getUrl')
        ->willReturn($single_data[0]);

      $single_data[0] = $link;
    }

  public function getRouteCollectionForRequest(Request $request) {
    return $this->routes;
  }

  /** * {@inheritdoc} */
  public function getRouteByName($name) {
    $routes = $this->getRoutesByNames([$name]);
    if (empty($routes)) {
      throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    return reset($routes);
  }

  /** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    // Nothing to do.   }

  
$locale = $parameters['_locale'] ?? $this->context->getParameter('_locale') ?: $this->defaultLocale;

        if (null !== $locale) {
            do {
                if (null !== ($route = $this->routes->get($name.'.'.$locale)) && $route->getDefault('_canonical_route') === $name) {
                    break;
                }
            } while (false !== $locale = strstr($locale, '_', true));
        }

        if (null === $route ??= $this->routes->get($name)) {
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
        }

        // the Route has a cache of its own and is not recompiled as long as it does not get modified         $compiledRoute = $route->compile();

        $defaults = $route->getDefaults();
        $variables = $compiledRoute->getVariables();

        if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
            if (!\in_array('_locale', $variables, true)) {
                unset($parameters['_locale']);
            }
$locale = $parameters['_locale'] ?? $this->context->getParameter('_locale') ?: $this->defaultLocale;

        if (null !== $locale) {
            do {
                if (null !== ($route = $this->routes->get($name.'.'.$locale)) && $route->getDefault('_canonical_route') === $name) {
                    break;
                }
            } while (false !== $locale = strstr($locale, '_', true));
        }

        if (null === $route ??= $this->routes->get($name)) {
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
        }

        // the Route has a cache of its own and is not recompiled as long as it does not get modified         $compiledRoute = $route->compile();

        $defaults = $route->getDefaults();
        $variables = $compiledRoute->getVariables();

        if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) {
            if (!\in_array('_locale', $variables, true)) {
                unset($parameters['_locale']);
            }
if ($alias->isDeprecated()) {
                    $deprecations[] = $deprecation = $alias->getDeprecation($currentId);
                    trigger_deprecation($deprecation['package']$deprecation['version']$deprecation['message']);
                }

                $visited[] = $currentId;
                $currentId = $alias->getId();
            }

            if (null === $target = $routes->get($currentId)) {
                throw new RouteNotFoundException(sprintf('Target route "%s" for alias "%s" does not exist.', $currentId$name));
            }

            $compiledTarget = $target->compile();

            $compiledAliases[$name] = [
                $compiledTarget->getVariables(),
                $target->getDefaults(),
                $target->getRequirements(),
                $compiledTarget->getTokens(),
                $compiledTarget->getHostTokens(),
                $target->getSchemes(),
                

  public function getRouteByName($name) {
    $routes = $this->getRoutesByNames([$name]);
    if (empty($routes)) {
      throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
    }

    return reset($routes);
  }

  /** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    if (empty($names)) {
      throw new \InvalidArgumentException('You must specify the route names to load');
    }
Home | Imprint | This part of the site doesn't use cookies.