In one of my controller file I have destroyed customer session (Instance of Magento\Customer\Model\Session
)and started it again due to some specific requirement.
public function execute()
{
$customerId = $this->session->getCustomer()->getId(); // 4
$this->session->destroy();
$this->session->start();
$this->session->setCid($customerId);
return $this->pageFactory->create();
}
And in another controller file I am trying to get session value as given below:
public function execute()
{
$customerId = $this->session->getCid();
var_dump($customerId);exit;
}
which gives NULL. If I am not destroying customer session in first controller then I am able to get session value in second controller properly. What I am missing?
$this->session->destroy();
is also deleting session variables set through PHP's $_SESSION. For example if I add $_SESSION['cid'] = 4;
after $this->session->destroy();
, I am not able to get it through echo $_SESSION['cid'];