-1

How to set custom session in my controller ? I'm set the session in controller but I got an interceptor error in Magento2.

1
  • 1
    post question with your specfic codes (what ever you did) and also post error description(Which are you facing). So that we can investigate your problem very closely and may we help you quickly. – Supravat Mondal Dec 12 '19 at 14:28
0

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

<?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>

Step 4 : Inject Custom Session Class in Controller

    <?php 
namespace Vendor/Modulename/Controller/Index/index;

class index extends /Magento/Framework/App/Action/Action
{
    public function __construct(
      ......................
      \MyModule\MySession\Model\Session $session,
      ......................
      array $data = []
    ) {
      $this->session = $session;
      parent::__construct($context,$data);
    }


    public function execute() {
        
        ................
        // Set Custom Session 
        $exampleArry = [
            'var1' => "first value",
            'var2' => "second value",
        ];
        $this->session->setExampleArry($exampleArry);
        
        // Get Custom Session
        $this->session->getExampleArry();

        // To get all session data : 
        $this->session->getData();


        ..................
    }
}

Step 5 : Run this command

php bin/magento setup:di:compile

php bin/magento cache:flush -q

5
  • thanks for ur answer,here i want to pass this session value to model .How can I do this? – Raji Dec 12 '19 at 14:58
  • Hi @supravat magento.stackexchange.com/questions/298536/… can u suggest me on this – Nagaraju K Dec 13 '19 at 4:31
  • @Raji, Inject Session class in your Model and call $this->session->setExampleArry($exampleArry); where you want. Don't forgat to compile your code. – Supravat Mondal Dec 13 '19 at 5:35
  • ok sir , thank you. – Raji Dec 14 '19 at 13:42
  • @Raji, accept answer if its really help you. So that it could be help other also. – Supravat Mondal Dec 14 '19 at 15:42
0

Steps to set Customer Session:

  1. You injection \Magento\Customer\Model\Session class to the constructor function of the Controller.
  2. Remove generated folder in magento source root by command: rm -rf generated/*
  3. Run again.

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.