You are here

public function AcceptHeaderMiddleware::handle in Zircon Profile 8

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

Handles a Request to convert it to a Response.

When $catch is true, the implementation must catch all exceptions and do its best to convert them to a Response instance.

Parameters

Request $request A Request instance:

int $type The type of the request: (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)

bool $catch Whether to catch exceptions or not:

Return value

Response A Response instance

Throws

\Exception When an Exception occurs during processing

Overrides HttpKernelInterface::handle

File

core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php, line 31
Contains \Drupal\accept_header_routing_test\AcceptHeaderMiddleware.

Class

AcceptHeaderMiddleware
Example implementation of accept header based content negotation.

Namespace

Drupal\accept_header_routing_test

Code

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);
}