class AcceptHeaderMiddleware in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php \Drupal\accept_header_routing_test\AcceptHeaderMiddleware
Example implementation of accept header based content negotation.
Hierarchy
- class \Drupal\accept_header_routing_test\AcceptHeaderMiddleware implements HttpKernelInterface
Expanded class hierarchy of AcceptHeaderMiddleware
File
- core/
modules/ system/ tests/ modules/ accept_header_routing_test/ src/ AcceptHeaderMiddleware.php, line 16 - Contains \Drupal\accept_header_routing_test\AcceptHeaderMiddleware.
Namespace
Drupal\accept_header_routing_testView source
class AcceptHeaderMiddleware implements HttpKernelInterface {
/**
* Constructs a new AcceptHeaderMiddleware instance.
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
* The app.
*/
public function __construct(HttpKernelInterface $app) {
$this->app = $app;
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
$mapping = [
'application/json' => 'json',
'application/hal+json' => 'hal_json',
'application/xml' => 'xml',
'text/html' => 'html',
];
$accept = $request->headers
->get('Accept') ?: [
'text/html',
];
if (isset($mapping[$accept[0]])) {
$request
->setRequestFormat($mapping[$accept[0]]);
}
return $this->app
->handle($request, $type, $catch);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcceptHeaderMiddleware:: |
public | function |
Handles a Request to convert it to a Response. Overrides HttpKernelInterface:: |
|
AcceptHeaderMiddleware:: |
public | function | Constructs a new AcceptHeaderMiddleware instance. | |
HttpKernelInterface:: |
constant | |||
HttpKernelInterface:: |
constant |