You are here

public function RouteProvider::getRouteCollectionForRequest in Drupal 10

Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::getRouteCollectionForRequest()
  2. 10 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getRouteCollectionForRequest()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getRouteCollectionForRequest()
  2. 9 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getRouteCollectionForRequest()

Finds routes that may potentially match the request.

This may return a mixed list of class instances, but all routes returned must extend the core symfony route. The classes may also implement RouteObjectInterface to link to a content document.

This method may not throw an exception based on implementation specific restrictions on the url. That case is considered a not found - returning an empty array. Exceptions are only used to abort the whole request in case something is seriously broken, like the storage backend being down.

Note that implementations may not implement an optimal matching algorithm, simply a reasonable first pass. That allows for potentially very large route sets to be filtered down to likely candidates, which may then be filtered in memory more completely.

Parameters

\Symfony\Component\HttpFoundation\Request $request: A request against which to match.

Return value

\Symfony\Component\Routing\RouteCollection RouteCollection with all urls that could potentially match $request. Empty collection if nothing can match. The collection will be sorted from highest to lowest fit (match of path parts) and then in ascending order by route name for routes with the same fit.

Overrides RouteProviderInterface::getRouteCollectionForRequest

File

core/lib/Drupal/Core/Routing/RouteProvider.php, line 163

Class

RouteProvider
A Route Provider front-end for all Drupal-stored routes.

Namespace

Drupal\Core\Routing

Code

public function getRouteCollectionForRequest(Request $request) {

  // Cache both the system path as well as route parameters and matching
  // routes.
  $cid = $this
    ->getRouteCollectionCacheId($request);
  if ($cached = $this->cache
    ->get($cid)) {
    $this->currentPath
      ->setPath($cached->data['path'], $request);
    $request->query
      ->replace($cached->data['query']);
    return $cached->data['routes'];
  }
  else {

    // Just trim on the right side.
    $path = $request
      ->getPathInfo();
    $path = $path === '/' ? $path : rtrim($request
      ->getPathInfo(), '/');
    $path = $this->pathProcessor
      ->processInbound($path, $request);
    $this->currentPath
      ->setPath($path, $request);

    // Incoming path processors may also set query parameters.
    $query_parameters = $request->query
      ->all();
    $routes = $this
      ->getRoutesByPath(rtrim($path, '/'));
    $cache_value = [
      'path' => $path,
      'query' => $query_parameters,
      'routes' => $routes,
    ];
    $this->cache
      ->set($cid, $cache_value, CacheBackendInterface::CACHE_PERMANENT, [
      'route_match',
    ]);
    return $routes;
  }
}