cancel
Showing results for 
Search instead for 
Did you mean: 

Create Custom session and Initialize it preDispatch

SOLVED

Create Custom session and Initialize it preDispatch

I am unable to find dev docs for creating Custom Session and Initialize it before PreDispatch,
Like Customer session, Core Session, and Checkout Session
Thanks
Sasikiran Kesavan

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Create Custom session and Initialize it preDispatch

Steps to create custom session and Initialize it preDispatch

 

Step 1 : Create Session.php file in Model

 

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Modulename\Model;
/**
 * Message session model
 */

class Session extends \Magento\Framework\Session\SessionManager
{
  protected $_session;
  protected $_coreUrl = null;
  protected $_configShare;
  protected $_urlFactory;
  protected $_eventManager;
  protected $response;
  protected $_sessionManager;
 
  public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Session\SidResolverInterface $sidResolver,
        \Magento\Framework\Session\Config\ConfigInterface $sessionConfig,
        \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
        \Magento\Framework\Session\ValidatorInterface $validator,
        \Magento\Framework\Session\StorageInterface $storage,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\App\State $appState,
        \Magento\Framework\Session\Generic $session,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\App\Response\Http $response
    ) {

        $this->_session = $session;
        $this->_eventManager = $eventManager;
 
        parent::__construct(
            $request,
            $sidResolver,
            $sessionConfig,
            $saveHandler,
            $validator,
            $storage,
            $cookieManager,
            $cookieMetadataFactory,
            $appState
        );
        $this->response = $response;
        $this->_eventManager->dispatch('sessionname_session_init', ['sessionname_session' => $this]);
    }  
}

 

Step 2:

Inside Model/Session create Storage.php

 

namespace Vendor\Modulename\Model\Session;

class Storage extends \Magento\Framework\Session\Storage
{
    /**
     * @param \Magento\Customer\Model\Config\Share $configShare
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param string $namespace
     * @param array $data
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        $namespace = 'sessionname',
        array $data = []
    ) {
        parent::__construct($namespace, $data);
    }
}

 

Step 3 :

Inside etc create di.xml

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:frameworkSmiley SurprisedbjectManager/etc/config.xsd">
    
    <type name="Vendor\Modulename\Model\Session">
        <arguments>
            <argument name="storage" xsi:type="object">Vendor\Modulename\Model\Session\Storage</argument>
        </arguments>
    </type>
</config>

View solution in original post

3 REPLIES 3

Re: Create Custom session and Initialize it preDispatch

Steps to create custom session and Initialize it preDispatch

 

Step 1 : Create Session.php file in Model

 

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Modulename\Model;
/**
 * Message session model
 */

class Session extends \Magento\Framework\Session\SessionManager
{
  protected $_session;
  protected $_coreUrl = null;
  protected $_configShare;
  protected $_urlFactory;
  protected $_eventManager;
  protected $response;
  protected $_sessionManager;
 
  public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Session\SidResolverInterface $sidResolver,
        \Magento\Framework\Session\Config\ConfigInterface $sessionConfig,
        \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
        \Magento\Framework\Session\ValidatorInterface $validator,
        \Magento\Framework\Session\StorageInterface $storage,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\App\State $appState,
        \Magento\Framework\Session\Generic $session,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\App\Response\Http $response
    ) {

        $this->_session = $session;
        $this->_eventManager = $eventManager;
 
        parent::__construct(
            $request,
            $sidResolver,
            $sessionConfig,
            $saveHandler,
            $validator,
            $storage,
            $cookieManager,
            $cookieMetadataFactory,
            $appState
        );
        $this->response = $response;
        $this->_eventManager->dispatch('sessionname_session_init', ['sessionname_session' => $this]);
    }  
}

 

Step 2:

Inside Model/Session create Storage.php

 

namespace Vendor\Modulename\Model\Session;

class Storage extends \Magento\Framework\Session\Storage
{
    /**
     * @param \Magento\Customer\Model\Config\Share $configShare
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param string $namespace
     * @param array $data
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        $namespace = 'sessionname',
        array $data = []
    ) {
        parent::__construct($namespace, $data);
    }
}

 

Step 3 :

Inside etc create di.xml

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:frameworkSmiley SurprisedbjectManager/etc/config.xsd">
    
    <type name="Vendor\Modulename\Model\Session">
        <arguments>
            <argument name="storage" xsi:type="object">Vendor\Modulename\Model\Session\Storage</argument>
        </arguments>
    </type>
</config>

Re: Create Custom session and Initialize it preDispatch

 

@laxmansawant,

 

Thanks for the detailed solution,

It is working if my cache is disabled. if i enable the cache, i am unable to fetch get values from custom session.

it is working in my custom module controller/action pages, 

But not working in product, category and home pages, when i try to get values from the block.

 

Thanks

Re: Create Custom session and Initialize it preDispatch

In your layout xml file just add

<block class="" name="" template="" cacheable="false"/>