3 namespace Drupal\accept_header_routing_test;
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpKernel\HttpKernelInterface;
9 * Example implementation of "accept header"-based content negotiation.
11 class AcceptHeaderMiddleware implements HttpKernelInterface {
14 * Constructs a new AcceptHeaderMiddleware instance.
16 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app
19 public function __construct(HttpKernelInterface $app) {
26 public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
28 'application/json' => 'json',
29 'application/hal+json' => 'hal_json',
30 'application/xml' => 'xml',
31 'text/html' => 'html',
34 $accept = $request->headers->get('Accept') ?: ['text/html'];
35 if (isset($mapping[$accept[0]])) {
36 $request->setRequestFormat($mapping[$accept[0]]);
39 return $this->app->handle($request, $type, $catch);