DynamicPageCacheTestController.php in Drupal 10
File
core/modules/dynamic_page_cache/tests/dynamic_page_cache_test/src/DynamicPageCacheTestController.php
View source
<?php
namespace Drupal\dynamic_page_cache_test;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DynamicPageCacheTestController {
use StringTranslationTrait;
public function response() {
return new Response('foobar');
}
public function cacheableResponse() {
$user = User::load(1);
$response = new CacheableResponse($user
->label());
$response
->addCacheableDependency($user);
return $response;
}
public function html() {
return [
'content' => [
'#markup' => 'Hello world.',
],
];
}
public function htmlWithCacheContexts(Request $request) {
$build = $this
->html();
$build['dynamic_part'] = [
'#markup' => $this
->t('Hello there, %animal.', [
'%animal' => $request->query
->get('animal'),
]),
'#cache' => [
'contexts' => [
'url.query_args:animal',
],
],
];
return $build;
}
public function htmlUncacheableMaxAge() {
$build = $this
->html();
$build['very_dynamic_part'] = [
'#markup' => 'Drupal cannot handle the awesomeness of llamas.',
'#cache' => [
'max-age' => 0,
],
];
return $build;
}
public function htmlUncacheableContexts() {
$build = $this
->html();
$build['very_dynamic_part'] = [
'#markup' => $this
->t('@username cannot handle the awesomeness of llamas.', [
'@username' => \Drupal::currentUser()
->getDisplayName(),
]),
'#cache' => [
'contexts' => [
'user',
],
],
];
return $build;
}
public function htmlUncacheableTags() {
$build = $this
->html();
$build['very_dynamic_part'] = [
'#markup' => 'Drupal cannot handle the awesomeness of llamas.',
'#cache' => [
'tags' => [
'current-temperature',
],
],
];
return $build;
}
}