function UrlMatcher::matchCollection
Tries to match a URL with a set of routes.
Parameters
string $pathinfo The path info to be parsed:
Throws
NoConfigurationException If no routing configuration could be found
ResourceNotFoundException If the resource could not be found
MethodNotAllowedException If the resource was found but the request method is not allowed
1 call to UrlMatcher::matchCollection()
- UrlMatcher::match in vendor/
symfony/ routing/ Matcher/ UrlMatcher.php - Tries to match a URL path with a set of routes.
2 methods override UrlMatcher::matchCollection()
- Router::matchCollection in core/
lib/ Drupal/ Core/ Routing/ Router.php - Tries to match a URL with a set of routes.
- TraceableUrlMatcher::matchCollection in vendor/
symfony/ routing/ Matcher/ TraceableUrlMatcher.php - Tries to match a URL with a set of routes.
File
-
vendor/
symfony/ routing/ Matcher/ UrlMatcher.php, line 110
Class
- UrlMatcher
- UrlMatcher matches URL based on a set of routes.
Namespace
Symfony\Component\Routing\MatcherCode
protected function matchCollection(string $pathinfo, RouteCollection $routes) : array {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === ($method = $this->context
->getMethod())) {
$method = 'GET';
}
$supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
$trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
$staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
$requiredMethods = $route->getMethods();
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' !== $staticPrefix && !str_starts_with($trimmedPathinfo, $staticPrefix)) {
continue;
}
$regex = $compiledRoute->getRegex();
$pos = strrpos($regex, '$');
$hasTrailingSlash = '/' === $regex[$pos - 1];
$regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
if (!preg_match($regex, $pathinfo, $matches)) {
continue;
}
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\\{[\\w\\x80-\\xFF]+\\}/?$#', $route->getPath());
if ($hasTrailingVar && ($hasTrailingSlash || null === ($m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingSlash) {
$matches = $m;
}
else {
$hasTrailingVar = false;
}
}
$hostMatches = [];
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context
->getHost(), $hostMatches)) {
continue;
}
$attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
$status = $this->handleRouteRequirements($pathinfo, $name, $route, $attributes);
if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods, true))) {
return $this->allow = $this->allowSchemes = [];
}
continue;
}
if ($route->getSchemes() && !$route->hasScheme($this->context
->getScheme())) {
$this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
continue;
}
if ($requiredMethods && !\in_array($method, $requiredMethods, true)) {
$this->allow = array_merge($this->allow, $requiredMethods);
continue;
}
return array_replace($attributes, $status[1] ?? []);
}
return [];
}