You are here

public function AcceptHeaderMiddleware::handle in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php \Drupal\accept_header_routing_test\AcceptHeaderMiddleware::handle()
  2. 9 core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php \Drupal\accept_header_routing_test\AcceptHeaderMiddleware::handle()

File

core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php, line 27

Class

AcceptHeaderMiddleware
Example implementation of "accept header"-based content negotiation.

Namespace

Drupal\accept_header_routing_test

Code

public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) : Response {
  $mapping = [
    'application/json' => '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);
}