3

I am creating an observer for an event in Magento 2 and followed all the steps in the documents I should follow. The observer is called after I registered it in the events.xml file. However, it doesn't work when I set a dependency in the constructor.

So, strangely enough, this code doesn't work:

<?php
namespace MyName\MyModule\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class MyObserver implements ObserverInterface
{
    protected $_storeManager;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ){
        $this->_storeManager = $storeManager;
    }

    public function execute(Observer $observer)
    {
        //Observer execution code...
        echo 'This is a test';
    }
}
?>

And when I leave the constructor empty, everything works fine:

public function __construct(){

}

This is the case for every dependency I try to load. Can anyone figure out what I am doing wrong?

Thanks!

5

Make sure that you are operating in developer mode, otherwise you would need to recompile (bin/magento setup:di:compile) after every changed dependency.

Check current mode:

bin/magento deploy:mode:show

Set developer mode:

bin/magento deploy:mode:developer

If you already are in developer mode, there might be some generated files left over from the previous mode, or because you ran setup:di:compile in developer mode (which you should not). You can delete them with:

rm -r var/generation/* var/di/*
0

As Fabian Schmengler said make sure you are working in Development mode.

When you create a class in module some code is generated in var/generation directory, so If you change the definition of the class in you module app/code/<vendor>/<module> the generated code in var/generation is now invalid, which result in exceptions/error.

So, If you have added new parameters to the constructor you have to do following things

  1. Delete var/generation directory
  2. reindex php bin/magento indexer:reindex
  3. clear cache php bin/magento cache:clean

Know more

http://devdocs.magento.com/guides/v2.0/howdoi/php/php_clear-dirs.html http://devdocs.magento.com/guides/v2.0/extension-dev-guide/code-generation.html

  • (2) and (3) are not helping, as the Magento indexer and cache have nothing to do with dependency injection and generated code. If you are in production mode, you need setup:di:compile, but during development you should not use production mode. – Fabian Schmengler Jul 24 '17 at 9:03
  • ya, that is a mistake on my side I was saying Development mode, don't know how I end up writing production instead of development mode. updated my answer – Arun Karnawat Jul 24 '17 at 12:29
  • after doing step-1, you have to re-index and clear cache, so that if there is any cache it will get deleted. if there is cache for the page then the code will not be regenerated in var/generation, so it is very important to deleted cache. – Arun Karnawat Jul 24 '17 at 12:32
  • But the index is irrelevant. – LM_Fielding Aug 9 '17 at 8:02
  • @LM_Fielding yes, the indexing is irrelevant, I know that now.. – Arun Karnawat Aug 9 '17 at 9:23

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.