42

What is the equivalent for session in Magento 1

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

Same in Magento 2?

3 Answers 3

32

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
3
  • 1
    Nice explanation! Commented Feb 5, 2018 at 5:37
  • @Sarfaraz, can we set session in controller and we can access in block file? Commented Jan 6, 2019 at 11:25
  • can we set integer value?, i am getting below error, Object of class Magento\\Framework\\Session\\Generic\\Interceptor could not be converted to string Commented Jan 8, 2019 at 2:25
65

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();
7
  • how to use session in phtml file? Commented Dec 18, 2015 at 9:23
  • @RakeshJesadiya. See my update.
    – Marius
    Commented Dec 18, 2015 at 9:28
  • 1
    @Bill. I don't know
    – Marius
    Commented Dec 18, 2015 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? Commented Dec 29, 2015 at 13:55
  • 2
    Yep. It's like in 1.9. Use unsMyValue
    – Marius
    Commented Dec 29, 2015 at 14:14
9

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();
11
  • @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? Commented Jun 27, 2017 at 5:56
  • 1
    That's fine, but you should attribute your sources when doing so
    – scrowler
    Commented Jun 27, 2017 at 6:38
  • 1
    @RobbieAverill, Yes you are right. Thank you for the suggestion. I updated my answer. Commented Jun 27, 2017 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 ? Commented Sep 7, 2017 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" Commented Sep 7, 2017 at 5:47

Your Answer

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

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