3

75 | WARNING | Session object MUST NOT be requested in constructor. | | It can only be passed as a method argument. 79 | WARNING | Session object MUST NOT be requested in constructor. | | It can only be passed as a method argument. 87 | WARNING | Request\Http object MUST NOT be requested in | | constructor. It can only be passed as a method | | argument.

And i have try this link but not solve my issue so please check it

Correct way to obtain a session object?

/**
 * Checkout session object
 *
 * @var \Magento\Checkout\Model\Session
 */
protected $checkoutSession;

/**
 * Constructor
 *
 * @param  \Magento\Checkout\Model\Session $checkoutSession
 * @return void
 */
public function __construct(\Magento\Checkout\Model\Session $checkoutSession)
{
    $this->checkoutSession = $checkoutSession;
}

Advance thanks for help.

  • How are you initializing it?Add you code snippet here. – Shireen N Feb 6 '18 at 10:33
  • now check updated code @Shireen N – Ravindrasinh Zala Feb 6 '18 at 10:39
1

Declare your session like this and check

<?php

    namespace frontname\modulename\Helper;

    use Magento\Checkout\Model\Session;

    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        public function __construct(
            Session $checkout,
            \Magento\Framework\App\Helper\Context $context
        ) {
            $this->checkout = $checkout;
            parent::__construct($context);
        }
        public function session()
        {
            $session = $this->checkout; 
        }
    }

Don't declare your session object directly inside the constructor

|improve this answer|||||
1

You must use the SessionFactory class instead(benefits listed here http://devdocs.magento.com/guides/v2.1/extension-dev-guide/code-generation.html) and its always a good coding practice to use the class first and then add it as a parameter inside a constructor

namespace Namespace\Module\..;

use Magento\Checkout\Model\SessionFactory as CheckoutSession;

class ClassName {
    ...

    protected $checkoutSession;

    public function __construct(
        ....
        CheckoutSession $checkoutSession,
        ....
    ){
        ....
        $this->checkoutSession = $checkoutSession->create();
        ....
    }
}
|improve this answer|||||

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