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?

Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183

3 Answers3

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();
Simon.Hermit
  • 79
  • 13
Marius
  • 197,939
  • 53
  • 422
  • 830
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
Sharfaraz Bheda
  • 1,323
  • 16
  • 28
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();
Prince Patel
  • 22,708
  • 10
  • 97
  • 119
  • @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 05:56
  • 1
    That's fine, but you should attribute your sources when doing so – scrowler Jun 27 '17 at 06:38
  • 1
    @RobbieAverill, Yes you are right. Thank you for the suggestion. I updated my answer. – Prince Patel Jun 27 '17 at 07: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 07 '17 at 05: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 07 '17 at 05:47
  • @SanjayGohil You can check my answer at https://magento.stackexchange.com/a/192304/35758 – Prince Patel Sep 07 '17 at 05:53
  • @PrincePatel: Can you please guide me through this, https://magento.stackexchange.com/questions/195914/magento-2-how-to-add-some-data-into-checkout-session – Abdul Moiz Oct 10 '17 at 11:41
  • @PrincePatel, I have set the session in controller, tried accessing in block file, value returning null for me – Jafar Pinjar Jan 06 '19 at 11:29
  • @PrincePatel, i am getting below error, Object of class Magento\Framework\Session\Generic\Interceptor could not be converted to string when i tried to set integer value in session – Jafar Pinjar Jan 08 '19 at 02:27
  • @PrincePatel, $this->_coreSession->start(); is it compulsory to use start there? – Jafar Pinjar Aug 16 '19 at 10:02
  • @jafarpinjar No, not compulsory to set or get value from the session. – Prince Patel Aug 16 '19 at 10:18