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でも同じですか?
回答:
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.phpMagento 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();