1

I need to save data in session on one page and retrieve same data on multiple pages. After saving the data when i refresh page Or redirect to another page data flush off . Can somebody explain me why it's happening? What can do to fix this?

I have tried with all types of sessions in magento2(customer,checkout,backend,catalog) also phpsession($_SESSION) without any luck.

am using Magento 2.1.7 with Luma Theme.

Any help will be really appreciated.

0

For Catalog, Checkout, Customer and other Magento session like

\Magento\Catalog\Model\Session
\Magento\Checkout\Model\Session
\Magento\Customer\Model\Session

Because Magento 2 is no longer using core session like in magento 1.

protected $customerSession;
public function __construct(
    ....
    \Magento\Customer\Model\Session $customerSession, 
    ....
){
    ....
    $this->customerSession = $customerSession;
    ....
}

To set and get the value of session.

$this->customerSession->setCustomerName('Bob'); //Set Value
$this->customerSession->getCustomerName(); // Get Value
$this->customerSession->unsCustomerName(); // Unset Value

You can use above Factory method by creating function or directly by using objectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerName('Bob');
$customerSession->getCustomerName();
  • 1
    Already tried this, didn't worked. I mentioned above. – Om Prakash Aug 18 '17 at 14:50
  • you want to get it in PHTML file? – Abhishek Panchal Aug 18 '17 at 14:50
  • No just in controller. Saving data in one controller and retrieve at multiple places . – Om Prakash Aug 18 '17 at 14:52
  • multiple places like other controllers ? right – Abhishek Panchal Aug 18 '17 at 14:53
  • Yes you are right. – Om Prakash Aug 18 '17 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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