You are here

class RequestFormatRouteFilter 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

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

Hierarchy

Expanded class hierarchy of RequestFormatRouteFilter

1 file declares its use of RequestFormatRouteFilter
RequestFormatRouteFilterTest.php in core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php
Contains \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest.
1 string reference to 'RequestFormatRouteFilter'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses RequestFormatRouteFilter
request_format_route_filter in core/core.services.yml
Drupal\Core\Routing\RequestFormatRouteFilter

File

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

Namespace

Drupal\Core\Routing
View source
class RequestFormatRouteFilter implements RouteFilterInterface {

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    return $route
      ->hasRequirement('_format');
  }

  /**
   * {@inheritdoc}
   */
  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}.");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestFormatRouteFilter::applies public function Determines if the route filter applies to the given route. Overrides RouteFilterInterface::applies
RequestFormatRouteFilter::filter public function Filters the route collection against a request and returns all matching routes. Overrides RouteFilterInterface::filter