什么是Magento 1中的会话等效项
Mage::getSingleton('core/session')->setMyValue('test');
Mage::getSingleton('core/session')->unsMyValue();
Magento 2一样吗?
什么是Magento 1中的会话等效项
Mage::getSingleton('core/session')->setMyValue('test');
Mage::getSingleton('core/session')->unsMyValue();
Magento 2一样吗?
Answers:
我在Magento2中找到了等效的方法:
Mage::getSingleton('core/session')->setMyValue('test');
Mage::getSingleton('core/session')->unsMyValue();
在核心会话中设置/获取/取消设置值:
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();
}
通过这种方式,如果我们的会话值与以下会话无关,我们可以设置自定义值:
在magento 2中,不再有core/session
。
尽管有这些(也可能是其他):
\Magento\Backend\Model\Session
\Magento\Catalog\Model\Session
\Magento\Checkout\Model\Session
\Magento\Customer\Model\Session
\Magento\Newsletter\Model\Session
您需要为块或控制器中所需的会话创建依赖关系。
让我们举个例子\Magento\Catalog\Model\Session
。
protected $catalogSession;
public function __construct(
....
\Magento\Catalog\Model\Session $catalogSession,
....
){
....
$this->catalogSession = $catalogSession;
....
}
然后,您可以在类内使用目录会话,如下所示:
$this->catalogSession->setMyValue('test');
$this->catalogSession->getMyValue();
[编辑]
您不应在模板中使用会话。
您应该在模板可以使用的块类中创建包装器,以设置/获取值。
使用上面的示例,在块中创建方法
public function setSessionData($key, $value)
{
return $this->catalogSession->setData($key, $value);
}
public function getSessionData($key, $remove = false)
{
return $this->catalogSession->getData($key, $remove);
}
但是,如果您真的想在模板中使用会话,则只需在代码块中创建一个包装即可获取会话:
public function getCatalogSession()
{
return $this->catalogSession;
}
然后,您可以在模板中执行以下操作:
$this->getCatalogSession()->setMyValue('test');
$this->getCatalogSession()->getMyValue();
unsMyValue
这些都是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
根据Magento 2 ECGM2编码标准,您首先使用会话类,然后可以将其传递给构造函数,否则将显示此错误
不得在构造函数中请求会话对象。它只能作为方法参数传递。
这是您可以在会话中设置和获取数据的方法
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;
}
}
设定值
$this->getCustomerSession()->setMyValue('YourValue');
获得价值
$this->getCustomerSession()->getMyValue();
对于未设置的会话值
$this->getCustomerSession()->unsMyValue();