Skip to main content

For Drupal 7:

For Drupal 7

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

For Drupal 8

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];
}

As usual, clean your cache once done.

Best practice (Drupal 8 | 9 | 10)

For performance and good practice reasons it is recommended to use dependency injection inside your Controller class.

modules/custom/my_module/MyController.php

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A controller class that respects Drupal coding standards.
 *
 * @see https://www.drupal.org/docs/develop/standards
 */
class MyController extends ControllerBase {

  /**
   * Page cache kill switch.
   *
   * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
   */
  protected $killSwitch;

  /**
   * {@inheritdoc}
   */
  public function __construct(KillSwitch $kill_switch) {
    $this->killSwitch = $kill_switch;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('page_cache_kill_switch')
    );
  }

  /**
   * Render my page with a renderable array.
   *
   * @return array
   */
  public function myPage() : array {
    $this->killSwitch->trigger();
    return [
      '#markup' => time(),
    ];
  }

}

For Drupal 7:

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];
}

As usual, clean your cache once done.

For Drupal 7

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];
}

As usual, clean your cache once done.

Best practice (Drupal 8 | 9 | 10)

For performance and good practice reasons it is recommended to use dependency injection inside your Controller class.

modules/custom/my_module/MyController.php

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A controller class that respects Drupal coding standards.
 *
 * @see https://www.drupal.org/docs/develop/standards
 */
class MyController extends ControllerBase {

  /**
   * Page cache kill switch.
   *
   * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
   */
  protected $killSwitch;

  /**
   * {@inheritdoc}
   */
  public function __construct(KillSwitch $kill_switch) {
    $this->killSwitch = $kill_switch;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('page_cache_kill_switch')
    );
  }

  /**
   * Render my page with a renderable array.
   *
   * @return array
   */
  public function myPage() : array {
    $this->killSwitch->trigger();
    return [
      '#markup' => time(),
    ];
  }

}

code grammar improvmetn. (just a couple of bits, really)
Source Link

For Drupal 7:

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];
}

}

And afterAs usual, clearclean your cache once done.

For Drupal 7:

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];

}

And after clear cache.

For Drupal 7:

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];
}

As usual, clean your cache once done.

Rollback to Revision 2
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

You should use code similar toFor Drupal 7:

Drupal has the following onefunction drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];

}

Recently, Add #cache[max-age] to disable caching and bubble the max-age was committed.
When $build['#cache']['max-age'] is not set, permanent cache is assumed. To indicate that a render array should not be cached at all, set $build['#cache']['max-age'] to 0 (i.e. 0 seconds); to indicate that a render array should be cached only for a limited period, e.g. 5 minutes, set $build['#cache']['max-age'] to 300And after (i.e. 5 * 60 seconds).

After, clear the cacheclear cache.

You should use code similar to the following one.

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];

}

Recently, Add #cache[max-age] to disable caching and bubble the max-age was committed.
When $build['#cache']['max-age'] is not set, permanent cache is assumed. To indicate that a render array should not be cached at all, set $build['#cache']['max-age'] to 0 (i.e. 0 seconds); to indicate that a render array should be cached only for a limited period, e.g. 5 minutes, set $build['#cache']['max-age'] to 300 (i.e. 5 * 60 seconds).

After, clear the cache.

For Drupal 7:

Drupal has the function drupal_page_is_cacheable() which can be used to set a page to uncacheable.

Here is the documentation: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_page_is_cacheable/7

For Drupal 8:

// Deny any page caching on the current request.    
\Drupal::service('page_cache_kill_switch')->trigger();

Then the code is:

public function myPage() {
  \Drupal::service('page_cache_kill_switch')->trigger();
  return [
    '#markup' => time(),
  ];

}

And after clear cache.

improved grammar and formatting
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Loading
Ajust the answer to the question. The question is for drupal 8.
Source Link
Loading
Source Link
Zuzuesque
  • 1.4k
  • 12
  • 15
Loading