拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Drupal9:如何禁用特定页面的快取

Drupal9:如何禁用特定页面的快取

白鹭 - 2022-01-25 1968 0 0

如何在Drupal 9中禁用特定页面的快取,我找到了一个解决方案,但它用于禁用所有页面的快取,解决方案是:在settings.yml中添加以下代码:

assert_options(ASSERT_ACTIVE, TRUE);
\Drupal\Component\Assertion\Handle::register();
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['page'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

那行得通,但我只想禁用特定页面的快取。谢谢。

uj5u.com热心网友回复:

在页面的my_company.routing.yml组态档中,您可以添加以下options.no_cache属性:

my_company.customer_detail:
  path: '/customer/{customer_entity}/detail'
  defaults:
    _controller: '\Drupal\my_company\Controller\CustomerController::detailAction'
    _title: 'Fiche client'
  requirements:
    _permission: 'access content'
    type: ^\d 
  options:
    no_cache: 'TRUE'

uj5u.com热心网友回复:

您可以使用hook_preprocess_HOOK来禁用快取。就象是:

function test_module_preprocess_node__landing_page(&$variables) {
  $variables['#cache']['max-age'] = 0;
}

哪里是test_module是你的模块名称,节点是你的物体型别和LANDING_PAGE为您的内容型别名称。这个钩子应该放在你的模块.module档案中。

此外,我建议您查看Cache API文章,您可以在其中找到如何控制站点快取以及如何使用快取背景关系和标签

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *