You are here

public function RequestFormatRouteFilter::filter in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php \Drupal\Core\Routing\RequestFormatRouteFilter::filter()

Filters the route collection against a request and returns all matching routes.

Parameters

RouteCollection $collection The collection against which to match.:

Request $request A Request object against which to match.:

Return value

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 RouteFilterInterface::filter

File

core/lib/Drupal/Core/Routing/RequestFormatRouteFilter.php, line 30
Contains \Drupal\Core\Routing\RequestFormatRouteFilter.

Class

RequestFormatRouteFilter
Provides a route filter, which filters by the request format.

Namespace

Drupal\Core\Routing

Code

public function filter(RouteCollection $collection, Request $request) {
  $format = $request
    ->getRequestFormat('html');

  /** @var \Symfony\Component\Routing\Route $route */
  foreach ($collection as $name => $route) {

    // If the route has no _format specification, we move it to the end. If it
    // does, then no match means the route is removed entirely.
    if ($supported_formats = array_filter(explode('|', $route
      ->getRequirement('_format')))) {
      if (!in_array($format, $supported_formats)) {
        $collection
          ->remove($name);
      }
    }
    else {
      $collection
        ->add($name, $route);
    }
  }
  if (count($collection)) {
    return $collection;
  }

  // We do not throw a
  // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
  // because we don't want to return a 404 status code, but rather a 406.
  throw new NotAcceptableHttpException("No route found for the specified format {$format}.");
}