public function MethodFilter::filter in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Routing/MethodFilter.php \Drupal\Core\Routing\MethodFilter::filter()
Filters the route collection against a request and returns all matching routes.
Parameters
\Symfony\Component\Routing\RouteCollection $collection: The collection against which to match.
\Symfony\Component\HttpFoundation\Request $request: A Request object against which to match.
Return value
\Symfony\Component\Routing\RouteCollection A non-empty RouteCollection of matched routes
Throws
ResourceNotFoundException If none of the routes in $collection matches $request. This is a performance optimization to not continue the match process when a match will no longer be possible.
Overrides FilterInterface::filter
File
- core/
lib/ Drupal/ Core/ Routing/ MethodFilter.php, line 17
Class
- MethodFilter
- Filters routes based on the HTTP method.
Namespace
Drupal\Core\RoutingCode
public function filter(RouteCollection $collection, Request $request) {
$method = $request
->getMethod();
$all_supported_methods = [];
foreach ($collection
->all() as $name => $route) {
$supported_methods = $route
->getMethods();
// A route not restricted to specific methods allows any method. If this
// is the case, we'll also have at least one route left in the collection,
// hence we don't need to calculate the set of all supported methods.
if (empty($supported_methods)) {
continue;
}
// If the GET method is allowed we also need to allow the HEAD method
// since HEAD is a GET method that doesn't return the body.
if (in_array('GET', $supported_methods, TRUE)) {
$supported_methods[] = 'HEAD';
}
if (!in_array($method, $supported_methods, TRUE)) {
$all_supported_methods = array_merge($supported_methods, $all_supported_methods);
$collection
->remove($name);
}
}
if (count($collection)) {
return $collection;
}
throw new MethodNotAllowedException(array_unique($all_supported_methods));
}