function TestController::format

Same name in other branches
  1. 8.9.x core/modules/system/tests/modules/conneg_test/src/Controller/TestController.php \Drupal\conneg_test\Controller\TestController::format()
  2. 10 core/modules/system/tests/modules/content_negotiation_test/src/Controller/TestController.php \Drupal\content_negotiation_test\Controller\TestController::format()
  3. 11.x core/modules/system/tests/modules/content_negotiation_test/src/Controller/TestController.php \Drupal\content_negotiation_test\Controller\TestController::format()

Returns different responses depending on the request format.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request

Return value

\Symfony\Component\HttpFoundation\Response The response.

1 string reference to 'TestController::format'
conneg_test.routing.yml in core/modules/system/tests/modules/conneg_test/conneg_test.routing.yml
core/modules/system/tests/modules/conneg_test/conneg_test.routing.yml

File

core/modules/system/tests/modules/conneg_test/src/Controller/TestController.php, line 43

Class

TestController
Test controller for content negotiation tests.

Namespace

Drupal\conneg_test\Controller

Code

public function format(Request $request) {
    switch ($request->getRequestFormat()) {
        case 'json':
            return new JsonResponse([
                'some' => 'data',
            ]);
        case 'xml':
            return new Response('<xml></xml>', Response::HTTP_OK, [
                'Content-Type' => 'application/xml',
            ]);
        default:
            return new Response($request->getRequestFormat());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.