Router
class Router extends UrlMatcher implements RequestMatcherInterface, RouterInterface (View source)
Router implementation in Drupal.
A router determines, for an incoming request, the active controller, which is a callable that creates a response.
It consists of several steps, of which each are explained in more details below:
- Get a collection of routes which potentially match the current request. This is done by the route provider. See ::getInitialRouteCollection().
- Filter the collection down further more. For example this filters out routes applying to other formats: See ::applyRouteFilters()
- Find the best matching route out of the remaining ones, by applying a regex. See ::matchCollection().
- Enhance the list of route attributes, for example loading entity objects. See ::applyRouteEnhancers().
This implementation uses ideas of the following routers:
- \Symfony\Cmf\Component\Routing\DynamicRouter
- \Drupal\Core\Routing\UrlMatcher
- \Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher
Properties
| protected CurrentPathStack | $currentPath | The current path. |
from UrlMatcher |
| protected RouteProviderInterface | $routeProvider | The route provider responsible for the first-pass match. |
|
| protected EnhancerInterface[] | $enhancers | The list of available enhancers. |
|
| protected FilterInterface[] | $filters | The list of available route filters. |
|
| protected UrlGeneratorInterface | $urlGenerator | The URL generator. |
Methods
Constructs a new Router.
{@inheritdoc}
{@inheritdoc}
Tries to match a URL with a set of routes.
Tries to match a URL with a set of routes.
Returns a collection of potential matching routes for a request.
Apply the route enhancers to the defaults, according to priorities.
Applies all route filters to a given route collection.
Reapplies the fit order to a RouteCollection object.
{@inheritdoc}
{@inheritdoc}
Details
__construct(RouteProviderInterface $route_provider, CurrentPathStack $current_path, UrlGeneratorInterface $url_generator)
Constructs a new Router.
finalMatch(RouteCollection $collection, Request $request)
No description
addRouteFilter(FilterInterface $route_filter)
Adds a route filter.
addRouteEnhancer(EnhancerInterface $route_enhancer)
Adds a route enhancer.
match($pathinfo)
{@inheritdoc}
matchRequest(Request $request)
{@inheritdoc}
protected array|null
matchCollection(string $pathinfo, RouteCollection $routes)
Tries to match a URL with a set of routes.
protected array|null
doMatchCollection(string $pathinfo, RouteCollection $routes, bool $case_sensitive)
Tries to match a URL with a set of routes.
This code is very similar to Symfony's UrlMatcher::matchCollection() but it supports case-insensitive matching. The static prefix optimization is removed as this duplicates work done by the query in RouteProvider::getRoutesByPath().
protected RouteCollection
getInitialRouteCollection(Request $request)
Returns a collection of potential matching routes for a request.
protected array
applyRouteEnhancers(array $defaults, Request $request)
Apply the route enhancers to the defaults, according to priorities.
protected RouteCollection
applyRouteFilters(RouteCollection $collection, Request $request)
Applies all route filters to a given route collection.
This method reduces the sets of routes further down, for example by checking the HTTP method.
protected RouteCollection
applyFitOrder(RouteCollection $collection)
Reapplies the fit order to a RouteCollection object.
Route filters can reorder route collections. For example, routes with an explicit _format requirement will be preferred. This can result in a less fit route being used. For example, as a result of filtering /user/% comes before /user/login. In order to not break this fundamental property of routes, we need to reapply the fit order. We also need to ensure that order within each group of the same fit is preserved.
getRouteCollection()
{@inheritdoc}
generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{@inheritdoc}