0

Please anyone see my code and resolve this issue

Fatal error: Uncaught TypeError: Argument 1 passed to MyClass::__construct() must implement interface Magento\Framework\Session\SessionManagerInterface, none given, called in /home/dukaania/public_html/testing2/vendor/magento/module-checkout/view/frontend/templates/cart/shipping.phtml on line 132 and defined in /home/dukaania/public_html/testing2/vendor/magento/module-checkout/view/frontend/templates/cart/shipping.phtml:109 Stack trace: #0

    use Magento\Framework\Session\SessionManagerInterface as CoreSession;

    class MyClass
    {

        protected $_coreSession;

        public function __construct(CoreSession $coreSession) 
        {
            $this->_coreSession = $coreSession;

        }

      public function setValue($total_cost){
        $this->_coreSession->start();
        $this->_coreSession->setMessage($total_cost);


    }

    public function getValue(){
        $this->_coreSession->start();
        return $this->_coreSession->getMessage();


    }


}

$myclas = new MyClass();

 $myclas.setValue($total_cost);     
 echo $myclas.getValue();
2
  • $myclas = new MyClass(); $myclas.setValue($total_cost); echo $myclas.getValue(); use is not good idea
    – Amit Bera
    Commented Oct 5, 2018 at 7:36
  • just want to echo varaible or get variable Commented Oct 5, 2018 at 7:37

4 Answers 4

3

You issue at $myclas = new MyClass();

To initialize this class you have to use the object manager or have to inject that class to your class.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$myclass = $objectManager->create('{NameSpaceofClass}\MyClass');
3
  • how can i echo myclass on same phtml file Commented Oct 5, 2018 at 7:47
  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $myclass = $objectManager->create('{NameSpaceofClass}\MyClass'); $myclas.setValue($total_cost); echo $myclas.getValue();
    – Amit Bera
    Commented Oct 5, 2018 at 7:59
  • NOte that you have set value and get value on same time, it does not work, set value of session , will reflect one page refresh as Session is server site element
    – Amit Bera
    Commented Oct 5, 2018 at 8:01
1

simply run di compile

php -dmemory_limit=6G bin/magento setup:di:compile

AND

php bin/magento cache:clean && php bin/magento cache:flush

OR just run this command

If you are using Magento 2.1.9 or less than clear/remove

rm -rf var/generation folder

for above magento version run below command

rm -rf generated

and check your output again

7
  • nothing happend same issue after run those commands Commented Oct 5, 2018 at 7:04
  • which magento version are you using?
    – Hitesh
    Commented Oct 5, 2018 at 7:05
  • If you are using Magento 2.1.9 or less than clear/remove var/generation folder
    – Hitesh
    Commented Oct 5, 2018 at 7:05
  • version magento 2...... Commented Oct 5, 2018 at 7:06
  • nothing happend Commented Oct 5, 2018 at 7:11
0

Please replace your constructor with this and let's check if it works,

 public function __construct(
       \Magento\Framework\Session\SessionManagerInterface $coreSession
    ) {
        $this->_coreSession = $coreSession;
    }

As we are directly passing the SessionManagerInterface class in the constructor we don't need to include it.

Now run the following commands

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

1
  • nothing gonna work same issue Commented Oct 5, 2018 at 7:35
0

Set/Get/Unset Value in Core Session:

protected $_coreSession;

public function __construct(
    -----
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ){
    $this->_coreSession = $coreSession;
    ----
}

public function setValue($total_cost){
    $this->_coreSession->start();
    $this->_coreSession->setMessage($total_cost);
}

public function getValue(){
    $this->_coreSession->start();
    return $this->_coreSession->getMessage();
}

public function unSetValue(){
    $this->_coreSession->start();
    return $this->_coreSession->unsMessage();
}

after this Run this SSH Command

  1. rm -rf var/*
  2. rm -rf generated/*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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