14

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.

4 Answers 4

23

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.

3
  • I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code. Jul 27, 2016 at 6:21
  • Please check modified answer added by object manager. Jul 27, 2016 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. Jul 27, 2016 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
0

I did what was written and it worked, but when I log out, it continues with the value I set in the session

0

If you just want to know if customer is logged in, you can inject httpContext to your block. For example:

namespace Magenteiro\CustomerSession\Block;

use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Http\Context;
use Magento\Framework\View\Element\Template;


class CustomerInfo extends Template
{
    public function __construct(Template\Context $context, protected Context $httpContext, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function getIsLoggedIn(): bool{
        return $this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
    }

}

And then use $this->isLoggedIn() in your block to know if customer is logged in.

In my tests it worked even with all caches turned on.

If you want to get more details from session, you may want to intercept httpContext and add them there.

More about this here.

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.