PageCacheAcceptHeaderController.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/system_test/src/Controller/PageCacheAcceptHeaderController.php
  2. 8.9.x core/modules/system/tests/modules/system_test/src/Controller/PageCacheAcceptHeaderController.php
  3. 11.x core/modules/system/tests/modules/system_test/src/Controller/PageCacheAcceptHeaderController.php

Namespace

Drupal\system_test\Controller

File

core/modules/system/tests/modules/system_test/src/Controller/PageCacheAcceptHeaderController.php

View source
<?php

namespace Drupal\system_test\Controller;

use Drupal\Core\Cache\CacheableAjaxResponse;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Defines a controller to respond the page cache accept header test.
 */
class PageCacheAcceptHeaderController {
    
    /**
     * Processes a request that will vary with Accept header.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The current request object.
     *
     * @return mixed
     */
    public function content(Request $request) {
        if ($request->getRequestFormat() === 'json' && $request->query
            ->get('_wrapper_format') === 'drupal_ajax') {
            $response = new CacheableAjaxResponse([
                'content' => 'oh hai this is ajax',
            ]);
        }
        elseif ($request->getRequestFormat() === 'json') {
            $response = new CacheableJsonResponse([
                'content' => 'oh hai this is json',
            ]);
        }
        else {
            $response = new CacheableResponse("<p>oh hai this is html.</p>");
        }
        $response->addCacheableDependency((new CacheableMetadata())->addCacheContexts([
            'url.query_args:_wrapper_format',
        ]));
        return $response;
    }

}

Classes

Title Deprecated Summary
PageCacheAcceptHeaderController Defines a controller to respond the page cache accept header test.

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