function RouteProvider::getRouteCollectionForRequest

Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::getRouteCollectionForRequest()
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::getRouteCollectionForRequest()
  2. 9 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getRouteCollectionForRequest()
  3. 8.9.x core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::getRouteCollectionForRequest()
  4. 8.9.x core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getRouteCollectionForRequest()
  5. 11.x core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::getRouteCollectionForRequest()
  6. 11.x 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.

File

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

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;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.