What is the equivalent for session in Magento 1

Mage::getSingleton('core/session')->setMyValue('test');
Mage::getSingleton('core/session')->unsMyValue();

Same in Magento 2?

up vote 17 down vote accepted

I found the equivalent way for this in Magento2:

Mage::getSingleton('core/session')->setMyValue('test');
Mage::getSingleton('core/session')->unsMyValue();

Set/Get/Unset Value in Core Session:

protected $_coreSession;

public function __construct(
    -----
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ){
    $this->_coreSession = $coreSession;
    ----
}

public function setValue(){
    $this->_coreSession->start();
    $this->_coreSession->setMessage('The Core session');
}

public function getValue(){
    $this->_coreSession->start();
    return $this->_coreSession->getMessage();
}

public function unSetValue(){
    $this->_coreSession->start();
    return $this->_coreSession->unsMessage();
}

By this way we can set custom values if our session value is not related to below sessions:

  • \Magento\Backend\Model\Session
  • \Magento\Catalog\Model\Session
  • \Magento\Checkout\Model\Session
  • \Magento\Customer\Model\Session
  • \Magento\Newsletter\Model\Session

In magento 2 there is no more core/session.
There are these ones though (may be others also):

  • \Magento\Backend\Model\Session
  • \Magento\Catalog\Model\Session
  • \Magento\Checkout\Model\Session
  • \Magento\Customer\Model\Session
  • \Magento\Newsletter\Model\Session

You need to create a dependency for the session you need in your block or controller or whatever.
Let's take for example \Magento\Catalog\Model\Session.

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

Then you can use the catalog session inside the class like this:

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

[EDIT]
You should not use sessions in templates.
You should create wrappers in the block class that the templates can use in order to set/get values.

Using the example above, create the methods in the block

public function setSessionData($key, $value)
{
    return $this->catalogSession->setData($key, $value);
}

public function getSessionData($key, $remove = false)
{
    return $this->catalogSession->getData($key, $remove);
}

But if you really want to use the session in the template you can just create a wrapper in your block for getting the session:

public function getCatalogSession()
{
    return $this->catalogSession;
}

Then you can do this in the template:

$this->getCatalogSession()->setMyValue('test');
$this->getCatalogSession()->getMyValue();
  • how to use session in phtml file? – Rakesh Jesadiya Dec 18 '15 at 9:23
  • @RakeshJesadiya. See my update. – Marius Dec 18 '15 at 9:28
  • 1
    @Bill. I don't know – Marius Dec 18 '15 at 9:38
  • 1
    @Marius I think you forgot to mention how to unset session variable. So please comment regarding it. Is it similar Magento 1.9.x.x or else? – Bhupendra Jadeja Dec 29 '15 at 13:55
  • 2
    Yep. It's like in 1.9. Use unsMyValue – Marius Dec 29 '15 at 14:14

These are all session types in Magento 2

1)  \Magento\Catalog\Model\Session //vendor/magento/module-catalog/Model/Session.php

2) \Magento\Newsletter\Model\Session //vendor/magento/module-newsletter/Model/Session.php

3) \Magento\Persistent\Model\Session //vendor/magento/module-persistent/Model/Session.php

4) \Magento\Customer\Model\Session //vendor/magento/module-customer/Model/Session.php

5) \Magento\Backend\Model\Session //vendor/magento/module-backend/Model/Session.php

6) \Magento\Checkout\Model\Session //vendor/magento/module-checkout/Model/Session.php

As per Magento 2 ECGM2 coding standard you first use session class then you can pass it into constructor otherwise this error will be shown

Session object MUST NOT be requested in constructor. It can only be passed as a method argument.

Here is how you can set and get data in session

namespace vendor\module\..;

use Magento\Catalog\Model\Session as CatalogSession;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Checkout\Model\Session as CheckoutSession;
use \Magento\Framework\Session\SessionManagerInterface as CoreSession

class ClassName {
    ...

    protected $_coreSession;
    protected $_catalogSession;
    protected $_customerSession;
    protected $_checkoutSession;

    public function __construct(
        ....
        CoreSession $coreSession,
        CatalogSession $catalogSession,
        CustomerSession $customerSession,
        CheckoutSession $checkoutSession,
        ....
    ){
        ....
        $this->_coreSession = $coreSession;
        $this->_catalogSession = $catalogSession;
        $this->_checkoutSession = $checkoutSession;
        $this->_customerSession = $customerSession;

        ....
    }

    public function getCoreSession() 
    {
        return $this->_coreSession;
    }

    public function getCatalogSession() 
    {
        return $this->_catalogSession;
    }

    public function getCustomerSession() 
    {
        return $this->_customerSession;
    }

    public function getCheckoutSession() 
    {
        return $this->_checkoutSession;
    }
}

To set value

$this->getCustomerSession()->setMyValue('YourValue');

To get value

$this->getCustomerSession()->getMyValue();

For Unset session value

$this->getCustomerSession()->unsMyValue();
  • @RobbieAverill If you found any solution from other sites you can share here on StackOverflow that's not called copy past. it's called R&D. Do you Understand? – Prince Patel Jun 27 '17 at 5:56
  • 1
    That's fine, but you should attribute your sources when doing so – Robbie Averill Jun 27 '17 at 6:38
  • 1
    @RobbieAverill, Yes you are right. Thank you for the suggestion. I updated my answer. – Prince Patel Jun 27 '17 at 7:34
  • I am getting warning while use a customerSession "Session object MUST NOT be requested in constructor. It can only be passed as a method argument." How to solve it ? – Sanjay Gohil Sep 7 '17 at 5:36
  • 1
    @SanjayGohil check my updated answer. first use session class and pass into constructor to avoid this error ""Session object MUST NOT be requested in constructor. It can only be passed as a method argument" – Prince Patel Sep 7 '17 at 5:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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