You are here

class DynamicPageCacheTestController in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/dynamic_page_cache/tests/dynamic_page_cache_test/src/DynamicPageCacheTestController.php \Drupal\dynamic_page_cache_test\DynamicPageCacheTestController

Controller routines for dynamic_page_cache_test routes.

Hierarchy

Expanded class hierarchy of DynamicPageCacheTestController

File

core/modules/dynamic_page_cache/tests/dynamic_page_cache_test/src/DynamicPageCacheTestController.php, line 19
Contains \Drupal\dynamic_page_cache_test\DynamicPageCacheTestController.

Namespace

Drupal\dynamic_page_cache_test
View source
class DynamicPageCacheTestController {
  use StringTranslationTrait;

  /**
   * A route returning a Response object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   A Response object.
   */
  public function response() {
    return new Response('foobar');
  }

  /**
   * A route returning a CacheableResponse object.
   *
   * @return \Drupal\Core\Cache\CacheableResponseInterface
   *   A CacheableResponseInterface object.
   */
  public function cacheableResponse() {
    $user = User::load(1);
    $response = new CacheableResponse($user
      ->label());
    $response
      ->addCacheableDependency($user);
    return $response;
  }

  /**
   * A route returning a render array (without cache contexts, so cacheable).
   *
   * @return array
   *   A render array.
   */
  public function html() {
    return [
      'content' => [
        '#markup' => 'Hello world.',
      ],
    ];
  }

  /**
   * A route returning a render array (with cache contexts, so cacheable).
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return array
   *   A render array.
   *
   * @see html()
   */
  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;
  }

  /**
   * A route returning a render array (with max-age=0, so uncacheable)
   *
   * @return array
   *   A render array.
   *
   * @see html()
   */
  public function htmlUncacheableMaxAge() {
    $build = $this
      ->html();
    $build['very_dynamic_part'] = [
      '#markup' => 'Drupal cannot handle the awesomeness of llamas.',
      '#cache' => [
        'max-age' => 0,
      ],
    ];
    return $build;
  }

  /**
   * A route returning a render array (with 'user' context, so uncacheable)
   *
   * @return array
   *   A render array.
   *
   * @see html()
   */
  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;
  }

  /**
   * A route returning a render array (with a cache tag preventing caching).
   *
   * @return array
   *   A render array.
   *
   * @see html()
   */
  public function htmlUncacheableTags() {
    $build = $this
      ->html();
    $build['very_dynamic_part'] = [
      '#markup' => 'Drupal cannot handle the awesomeness of llamas.',
      '#cache' => [
        'tags' => [
          'current-temperature',
        ],
      ],
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DynamicPageCacheTestController::cacheableResponse public function A route returning a CacheableResponse object.
DynamicPageCacheTestController::html public function A route returning a render array (without cache contexts, so cacheable).
DynamicPageCacheTestController::htmlUncacheableContexts public function A route returning a render array (with 'user' context, so uncacheable)
DynamicPageCacheTestController::htmlUncacheableMaxAge public function A route returning a render array (with max-age=0, so uncacheable)
DynamicPageCacheTestController::htmlUncacheableTags public function A route returning a render array (with a cache tag preventing caching).
DynamicPageCacheTestController::htmlWithCacheContexts public function A route returning a render array (with cache contexts, so cacheable).
DynamicPageCacheTestController::response public function A route returning a Response object.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.