class ContentTypeHeaderMatcher
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php \Drupal\Core\Routing\ContentTypeHeaderMatcher
- 10 core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php \Drupal\Core\Routing\ContentTypeHeaderMatcher
- 11.x core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php \Drupal\Core\Routing\ContentTypeHeaderMatcher
Filters routes based on the HTTP Content-type header.
Hierarchy
- class \Drupal\Core\Routing\ContentTypeHeaderMatcher implements \Drupal\Core\Routing\FilterInterface
Expanded class hierarchy of ContentTypeHeaderMatcher
1 file declares its use of ContentTypeHeaderMatcher
- ContentTypeHeaderMatcherTest.php in core/
tests/ Drupal/ Tests/ Core/ Routing/ ContentTypeHeaderMatcherTest.php
1 string reference to 'ContentTypeHeaderMatcher'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses ContentTypeHeaderMatcher
File
-
core/
lib/ Drupal/ Core/ Routing/ ContentTypeHeaderMatcher.php, line 12
Namespace
Drupal\Core\RoutingView source
class ContentTypeHeaderMatcher implements FilterInterface {
/**
* {@inheritdoc}
*/
public function filter(RouteCollection $collection, Request $request) {
// The Content-type header does not make sense on GET or DELETE requests,
// because they do not carry any content. Nothing to filter in this case.
// Same for all other safe methods.
if ($request->isMethodSafe() || $request->isMethod('DELETE')) {
return $collection;
}
$format = $request->getContentType();
foreach ($collection as $name => $route) {
$supported_formats = array_filter(explode('|', $route->getRequirement('_content_type_format') ?? ''));
if (empty($supported_formats)) {
// No restriction on the route, so we move the route to the end of the
// collection by re-adding it. That way generic routes sink down in the
// list and exact matching routes stay on top.
$collection->add($name, $route);
}
elseif (!in_array($format, $supported_formats)) {
$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 415.
if (!$request->headers
->get('Content-Type', FALSE)) {
throw new UnsupportedMediaTypeHttpException('No "Content-Type" request header specified');
}
else {
throw new UnsupportedMediaTypeHttpException('No route found that matches "Content-Type: ' . $request->headers
->get('Content-Type') . '"');
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ContentTypeHeaderMatcher::filter | public | function | Filters the route collection against a request. | Overrides FilterInterface::filter |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.