我有一個動態設置Cookie值'Drupal_visitor_country'的drupal 8網站。但是這個值正在被緩存,我無法在頁面刷新過程中正確地檢索它。Drupal 8 Cookie緩存問題
我在theme_preprocess_menu函數上使用此值,但它始終返回緩存的cookie值而不是實際值。有什麼辦法可以克服這種情況嗎?
任何幫助將不勝感激。
感謝
我有一個動態設置Cookie值'Drupal_visitor_country'的drupal 8網站。但是這個值正在被緩存,我無法在頁面刷新過程中正確地檢索它。Drupal 8 Cookie緩存問題
我在theme_preprocess_menu函數上使用此值,但它始終返回緩存的cookie值而不是實際值。有什麼辦法可以克服這種情況嗎?
任何幫助將不勝感激。
感謝
使用EventSubscriber: $events[KernelEvents::REQUEST][] = ['onRequest'];
/**
* @file
* Contains \Drupal\kvantstudio\EventSubscriber\KvantstudioEventSubscriber.
*/
namespace Drupal\kvantstudio\EventSubscriber;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Event Subscriber KvantstudioEventSubscriber.
*/
class KvantstudioEventSubscriber implements EventSubscriberInterface {
/**
* Code that should be triggered on event specified
*/
public function onRequest(GetResponseEvent $event) {
if (!isset($_COOKIE['Drupal_visitor_userHash'])) {
$uuid = \Drupal::service('uuid');
user_cookie_save(['userHash' => 'user-' . $uuid->generate()]);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [];
$events[KernelEvents::REQUEST][] = ['onRequest'];
return $events;
}
}
對我來說Middleware API的伎倆。見下文。
沒有工作:試圖使用KernelEvents - 他們沒有在頁面請求時觸發。 KernelEvents::REQUEST
根據資產請求進行了操作。
沒有工作:試圖濫用services.yml
,但發現我實際上無法在此排除頁面。
renderer.config:
required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions', ... ]
沒有工作:然後我試圖創建自己的CacheContext。發現它也沒有提供排除的方法。充其量,您可以爲特定節點的每個負載生成唯一的緩存,但這會導致緩存失效。 How to recognize, discover and create?另外:CacheContextInterface
做過的工作:Middleware API。它在Drupal緩存之前執行。 最好是從頁面緩存開始:
請注意,在中間件處理期間,Drupal可能無法完全引導,並且某些功能可能不存在。
謝謝Mario Vercellotti指點我正確的方向!
我有類似的情況,但KernelEvents :: REQUEST事件似乎只在非緩存頁面上執行,所以它對我沒有幫助。 – Rax