2

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'];

1
  • Is it working for you? Jun 3, 2018 at 18:39

1 Answer 1

1
+50

=> Create session in your first controller :

<?php

namespace Company\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action {
    public $customerSession;
    /**
     * @param Context $context
     */
    public function __construct(
        ..................................
        Context $context,
        \Magento\Customer\Model\Session $customerSession,
        PageFactory $resultPageFactory
        ..................................
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->customerSession = $customerSession;
    }

    public function execute() {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Company Module Controller 1'));
        $this->customerSession->setMyValue('test value');
        $this->customerSession->destroy();
        $this->customerSession->start();
        $this->customerSession->setMyValue('regenerate test value');
        return $resultPage;
    }
}
?>

=> For Getting session value in your second controller :

<?php

namespace Company\Module\Controller\Save;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Save extends Action {

    public $customerSession;

    /**
     * @param Context $context
     */
    public function __construct(
        ..................................
        Context $context,
        \Magento\Customer\Model\Session $customerSession,
        PageFactory $resultPageFactory
        ..................................
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->customerSession = $customerSession;
    }

    public function execute() {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Company Module Controller 2'));
        $this->customerSession->start(); // Refresh session before getting value
        var_dump($this->customerSession->getMyValue());
        return $resultPage;
    }
}

Hope this answer will helpful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.