如何在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文章,您可以在其中找到如何控制站點快取以及如何使用快取背景關系和標簽
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/397002.html
