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:

  1. Get a collection of routes which potentially match the current request. This is done by the route provider. See ::getInitialRouteCollection().
  2. Filter the collection down further more. For example this filters out routes applying to other formats: See ::applyRouteFilters()
  3. Find the best matching route out of the remaining ones, by applying a regex. See ::matchCollection().
  4. Enhance the list of route attributes, for example loading entity objects. See ::applyRouteEnhancers().

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

__construct(RouteProviderInterface $route_provider, CurrentPathStack $current_path, UrlGeneratorInterface $url_generator)

Constructs a new Router.

finalMatch(RouteCollection $collection, Request $request)

No description

getAttributes(Route $route, $name, array $attributes)

{@inheritdoc}

addRouteFilter(FilterInterface $route_filter)

Adds a route filter.

addRouteEnhancer(EnhancerInterface $route_enhancer)

Adds a route enhancer.

match($pathinfo)

{@inheritdoc}

matchRequest(Request $request)

{@inheritdoc}

array|null
matchCollection(string $pathinfo, RouteCollection $routes)

Tries to match a URL with a set of routes.

array|null
doMatchCollection(string $pathinfo, RouteCollection $routes, bool $case_sensitive)

Tries to match a URL with a set of routes.

RouteCollection
getInitialRouteCollection(Request $request)

Returns a collection of potential matching routes for a request.

array
applyRouteEnhancers(array $defaults, Request $request)

Apply the route enhancers to the defaults, according to priorities.

RouteCollection
applyRouteFilters(RouteCollection $collection, Request $request)

Applies all route filters to a given route collection.

RouteCollection
applyFitOrder(RouteCollection $collection)

Reapplies the fit order to a RouteCollection object.

getRouteCollection()

{@inheritdoc}

generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)

{@inheritdoc}

Details

__construct(RouteProviderInterface $route_provider, CurrentPathStack $current_path, UrlGeneratorInterface $url_generator)

Constructs a new Router.

Parameters

RouteProviderInterface $route_provider

The route provider.

CurrentPathStack $current_path

The current path.

UrlGeneratorInterface $url_generator

The URL generator.

finalMatch(RouteCollection $collection, Request $request)

No description

Parameters

RouteCollection $collection
Request $request

protected getAttributes(Route $route, $name, array $attributes)

{@inheritdoc}

Parameters

Route $route
$name
array $attributes

addRouteFilter(FilterInterface $route_filter)

Adds a route filter.

Parameters

FilterInterface $route_filter

The route filter.

addRouteEnhancer(EnhancerInterface $route_enhancer)

Adds a route enhancer.

Parameters

EnhancerInterface $route_enhancer

The route enhancer.

match($pathinfo)

{@inheritdoc}

Parameters

$pathinfo

matchRequest(Request $request)

{@inheritdoc}

Parameters

Request $request

protected array|null matchCollection(string $pathinfo, RouteCollection $routes)

Tries to match a URL with a set of routes.

Parameters

string $pathinfo

The path info to be parsed

RouteCollection $routes

The set of routes.

Return Value

array|null

An array of parameters. NULL when there is no match.

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().

Parameters

string $pathinfo

The path info to be parsed

RouteCollection $routes

The set of routes.

bool $case_sensitive

Determines if the match should be case-sensitive of not.

Return Value

array|null

An array of parameters. NULL when there is no match.

See also

\Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection()
RouteProvider::getRoutesByPath

protected RouteCollection getInitialRouteCollection(Request $request)

Returns a collection of potential matching routes for a request.

Parameters

Request $request

The current request.

Return Value

RouteCollection

The initial fetched route collection.

protected array applyRouteEnhancers(array $defaults, Request $request)

Apply the route enhancers to the defaults, according to priorities.

Parameters

array $defaults

The defaults coming from the final matched route.

Request $request

The request.

Return Value

array

The request attributes after applying the enhancers. This might consist raw values from the URL but also upcasted values, like entity objects, from route enhancers.

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.

Parameters

RouteCollection $collection

The route collection.

Request $request

The request.

Return Value

RouteCollection

The filtered/sorted route collection.

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.

Parameters

RouteCollection $collection

The route collection.

Return Value

RouteCollection

The reordered route collection.

getRouteCollection()

{@inheritdoc}

generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)

{@inheritdoc}

Parameters

$name
$parameters
$referenceType