11

I am struggling with magento 2 session. I have created below controller file as a sample code.

<?php
namespace vendor_name\module_name\Controller\SetGetSession;

use Magento\Framework\App\Action\Action;

class SetGetSession extends Action
{
    protected $customerSession;

    public function _construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }   

    public function execute()
    {

    }
}

Can anyone please help me with how to assign data and retrieve it from session variable?

Thank you.

15

You can Set and get Customer session by using Magento\Customer\Model\Session

protected $customerSession;

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

$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();

Or by object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
  1. Setting an information to the customer session:
$om = \Magento\Framework\App\ObjectManager::getInstance(); $session =
$om->get('Magento\Customer\Model\Session');  
$session->setTestKey('test value');
  1. Getting an information from the customer session:
$om = \Magento\Framework\App\ObjectManager::getInstance();  $session =
$om->get('Magento\Customer\Model\Session');
echo $session->getTestKey();

Session will extends core class Magento\Framework\Session\SessionManager to handle the session.

Hope this answer will help you.

  • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code. – Aniket Shinde Jul 27 '16 at 6:21
  • Please check modified answer added by object manager. – Krishna ijjada Jul 27 '16 at 6:27
  • Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question. – Aniket Shinde Jul 27 '16 at 6:34
3

You need to inject \Magento\Customer\Model\Session class for set and get data in customer session

Using Dependency Injection

protected $customerSession;

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

public function setValue()
{
    return $this->customerSession->setMyValue('YourValue'); //set value in customer session
}

public function getValue()
{
    return $this->customerSession->getMyValue(); //Get value from customer session
}

Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session

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.