How to Set, Retrieve and Unset Session Variables in Magento 2?

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();

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

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();