1. 8.5.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher
  2. 8.0.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher
  3. 8.1.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher
  4. 8.2.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher
  5. 8.3.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher
  6. 8.4.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher
  7. 8.6.x core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php AcceptHeaderMatcher

Filters routes based on the media type specified in the HTTP Accept headers.

Hierarchy

Expanded class hierarchy of AcceptHeaderMatcher

1 file declares its use of AcceptHeaderMatcher
AcceptHeaderMatcherTest.php in core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php
1 string reference to 'AcceptHeaderMatcher'
accept_header_routing_test.services.yml in core/modules/system/tests/modules/accept_header_routing_test/accept_header_routing_test.services.yml
core/modules/system/tests/modules/accept_header_routing_test/accept_header_routing_test.services.yml
1 service uses AcceptHeaderMatcher

File

core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php, line 14

Namespace

Drupal\accept_header_routing_test\Routing
View source
class AcceptHeaderMatcher implements RouteFilterInterface {

  /**
   * {@inheritdoc}
   */
  public function filter(RouteCollection $collection, Request $request) {

    // Generates a list of Symfony formats matching the acceptable MIME types.
    // @todo replace by proper content negotiation library.
    $acceptable_mime_types = $request
      ->getAcceptableContentTypes();
    $acceptable_formats = array_filter(array_map([
      $request,
      'getFormat',
    ], $acceptable_mime_types));
    $primary_format = $request
      ->getRequestFormat();
    foreach ($collection as $name => $route) {

      // _format could be a |-delimited list of supported formats.
      $supported_formats = array_filter(explode('|', $route
        ->getRequirement('_format')));
      if (empty($supported_formats)) {

        // No format restriction on the route, so it always matches. Move it to
        // the end of the collection by re-adding it.
        $collection
          ->add($name, $route);
      }
      elseif (in_array($primary_format, $supported_formats)) {

        // Perfect match, which will get a higher priority by leaving the route
        // on top of the list.
      }
      elseif (in_array('*/*', $acceptable_mime_types) || array_intersect($acceptable_formats, $supported_formats)) {

        // Move it to the end of the list.
        $collection
          ->add($name, $route);
      }
      else {

        // Remove the route if it does not match at all.
        $collection
          ->remove($name);
      }
    }
    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 formats ' . implode(' ', $acceptable_mime_types));
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    return TRUE;
  }

}

Members

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