4

In Magento 2 we have different classes to access session, depending on context we want to access it, for example \Magento\Customer\Model\Session or \Magento\Checkout\Model\Session. Recently I was wondering if in case of some bigger custom modules, when there is a need to use some session variables and so on it's good idea to implement custom session class to access php session. If there a sense to do it, has anyone tried to do it, and can share some code or point some important issues while doing it.

4

Add following config into global di.xml [SR/StackExchange/etc/di.xml]

<virtualType name="SR\StackExchange\Model\Session\Storage" type="Magento\Framework\Session\Storage">
    <arguments>
        <argument name="namespace" xsi:type="string">stackexchange</argument>
    </arguments>
</virtualType>
<type name="SR\StackExchange\Model\Session">
    <arguments>
        <argument name="storage" xsi:type="object">SR\StackExchange\Model\Session\Storage</argument>
    </arguments>
</type>

SR/StackExchange/Model/Session.php

namespace SR\StackExchange\Model;

class Session extends \Magento\Framework\Session\SessionManager
{

}
  • 1
    I think you missed the part of the question that asks whether there is a reason to do this? – Robbie Averill Jan 15 '17 at 18:28
  • @Sohel Rana - thanks for example - it's useful, but do you think that there are sometimes reason to do it? Any real example when it can be really useful or when it is not necessary and adds not needed complecity? – Bartosz Kubicki Jan 15 '17 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.