2016-09-19 113 views
5

我寫了一個magento控制器,它在客戶會話內存儲了一些過濾器信息。Magento 2丟棄所有會話參數

據Magento的2文檔我使用依賴注入讓Magento的生成我的會話對象:

/** 
* @var \Magento\Catalog\Model\Session 
*/ 
protected $_filterSession; 

/** 
* @param \Magento\Customer\Model\Session $filterSession 
*/ 
public function __construct(\Magento\Customer\Model\Session $filterSession) 
{ 
    $this->_filterSession = $filterSession; 
} 

注射過程是合作得非常好。我可以訪問會話,在其中存儲一個變量並在進一步的調用中返回它。

但是magento似乎不時丟棄整個會話信息。我不能準確地確定magento丟棄信息的時刻,它似乎是隨機的。

下面是代碼:

$this->_filterSession->setFrequency($frequency); 
$frequency = $this->_filterSession->getFrequency(); 

我嘗試了不同的會話作用域但行爲是一樣的。

經過多次嘗試,我嘗試使用PHP會話來存儲信息,但即使這個會話不時被丟棄。

我不知道我在做什麼錯,或者可能是這種奇怪行爲的原因。其他人是否有類似的問題或想法是什麼原因?

由於提前, 托馬斯

回答

2

這當瀏覽器丟失會話cookie通常發生。在變量丟失時,您應該檢查會話期間是否更改域名。或者用不同的瀏覽器。可能是一些行爲不當的瀏覽器插件。或者一些Magento擴展。許多Mageno 2擴展目前寫得不好。

+0

我認爲你是對的。我無法確定哪個擴展可以做到這一點,但是由於會話參數丟失了從magento本身使用的會話參數,所以必須有錯誤的擴展。 – thomas

2

我有一個類似的問題,使用PHP。我已經設置了session.referer_check。所以,當一個用戶來自外部頁面時,會話丟失了。如果這是你的問題,只需ini_set('session.referer_check', '');

+0

感謝您的回答。但是當我在同一個標​​籤內的同一頁面上導航時,也會出現這種現象。無論如何檢查了它。沒有解決問題。 – thomas

0

我沒有找到問題本身的解決方案,但避免了它。

對於那些誰也遇到這個問題,這裏是我的創可貼修復:

我介紹了一個新的cookie

public function getFrequency() 
{ 
    $frequency = $this->_cookieManager->getCookie(self::FREQUENCY_SESSION_KEY); 


    if(!isset($frequency)) 
    { 
     $frequency = self::FREQUENCY_DEFAULT_VALUE; 
    } 

    return $frequency; 
} 

public function setFrequency($frequency) 
{ 
    $metadata = $this->_cookieMetadataFactory 
     ->createPublicCookieMetadata() 
     ->setPath($this->_sessionManager->getCookiePath()) 
     ->setDomain($this->_sessionManager->getCookieDomain()); 

    $this->_cookieManager->setPublicCookie(
     self::FREQUENCY_SESSION_KEY, 
     $frequency, 
     $metadata 
    ); 
} 

進一步的細節,我建議你看看這個thread

問候,托馬斯