3 namespace Drupal\dynamic_page_cache_test;
5 use Drupal\Core\Cache\CacheableResponse;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drupal\user\Entity\User;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\Response;
12 * Controller routines for dynamic_page_cache_test routes.
14 class DynamicPageCacheTestController {
16 use StringTranslationTrait;
19 * A route returning a Response object.
21 * @return \Symfony\Component\HttpFoundation\Response
24 public function response() {
25 return new Response('foobar');
29 * A route returning a CacheableResponse object.
31 * @return \Drupal\Core\Cache\CacheableResponseInterface
32 * A CacheableResponseInterface object.
34 public function cacheableResponse() {
35 $user = User::load(1);
36 $response = new CacheableResponse($user->label());
37 $response->addCacheableDependency($user);
42 * A route returning a render array (without cache contexts, so cacheable).
47 public function html() {
50 '#markup' => 'Hello world.',
56 * A route returning a render array (with cache contexts, so cacheable).
58 * @param \Symfony\Component\HttpFoundation\Request $request
59 * The current request.
66 public function htmlWithCacheContexts(Request $request) {
67 $build = $this->html();
68 $build['dynamic_part'] = [
69 '#markup' => $this->t('Hello there, %animal.', ['%animal' => $request->query->get('animal')]),
72 'url.query_args:animal',
80 * A route returning a render array (with max-age=0, so uncacheable)
87 public function htmlUncacheableMaxAge() {
88 $build = $this->html();
89 $build['very_dynamic_part'] = [
90 '#markup' => 'Drupal cannot handle the awesomeness of llamas.',
99 * A route returning a render array (with 'user' context, so uncacheable)
106 public function htmlUncacheableContexts() {
107 $build = $this->html();
108 $build['very_dynamic_part'] = [
109 '#markup' => $this->t('@username cannot handle the awesomeness of llamas.', ['@username' => \Drupal::currentUser()->getDisplayName()]),
120 * A route returning a render array (with a cache tag preventing caching).
127 public function htmlUncacheableTags() {
128 $build = $this->html();
129 $build['very_dynamic_part'] = [
130 '#markup' => 'Drupal cannot handle the awesomeness of llamas.',
133 'current-temperature',