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
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
-
1Nice explanation!Himmat Paliwal– Himmat Paliwal2018年02月05日 05:37:31 +00:00Commented Feb 5, 2018 at 5:37
-
@Sarfaraz, can we set session in controller and we can access in block file?Jafar Pinjar– Jafar Pinjar2019年01月06日 11:25:34 +00:00Commented 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 stringJafar Pinjar– Jafar Pinjar2019年01月08日 02:25:14 +00:00Commented Jan 8, 2019 at 2:25
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– Rakesh Jesadiya2015年12月18日 09:23:41 +00:00Commented Dec 18, 2015 at 9:23
-
@RakeshJesadiya. See my update.Marius– Marius2015年12月18日 09:28:22 +00:00Commented Dec 18, 2015 at 9:28
-
1
-
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– Bhupendra Jadeja2015年12月29日 13:55:44 +00:00Commented Dec 29, 2015 at 13:55
-
2Yep. It's like in 1.9. Use
unsMyValueMarius– Marius2015年12月29日 14:14:43 +00:00Commented Dec 29, 2015 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– Prince Patel2017年06月27日 05:56:36 +00:00Commented Jun 27, 2017 at 5:56
-
1That's fine, but you should attribute your sources when doing soscrowler– scrowler2017年06月27日 06:38:01 +00:00Commented Jun 27, 2017 at 6:38
-
1@RobbieAverill, Yes you are right. Thank you for the suggestion. I updated my answer.Prince Patel– Prince Patel2017年06月27日 07:34:24 +00:00Commented 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 ?Sanjay Gohil– Sanjay Gohil2017年09月07日 05:36:06 +00:00Commented 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"Prince Patel– Prince Patel2017年09月07日 05:47:41 +00:00Commented Sep 7, 2017 at 5:47