I am new to Magento and was trying tutorial's example, and my site got broken with HTTP ERROR 500 when i have added my events.xml under my module's etc folder

<event name="controller_action_predispatch">
    <observer name="predispatch" instance="MyCompany\Test\Observer\ControlerPreDispatch" shared="false" />

along with my observer and here is the constructor

public function __construct(\Magento\Framework\App\Response\RedirectInterface $redirect,\Magento\Framework\App\ActionFlag $actionFlag)

Here is apache error:

PHP Fatal error:  `Uncaught TypeError: Argument 1 passed to MyCompany\\Test\\Observer\\ControlerPreDispatch::__construct() must implement interface Magento\\Framework\\App\\Response\\RedirectInterface,` .....

any clue?

  • 1
    Can you post your full code lines of your Observer? Feb 20, 2017 at 10:58
  • did you clear var/generation? Feb 20, 2017 at 12:55
  • namespace MyCompany\Test\Observer; use Magento\Framework\Event\ObserverInterface; class ControlerPreDispatch implements ObserverInterface { protected $redirect; protected $_actionFlag; public function __construct(\Magento\Framework\App\Response\RedirectInterface $redirect,\Magento\Framework\App\ActionFlag $actionFlag) { $this->_actionFlag = $actionFlag; }
    – Bashir
    Feb 21, 2017 at 18:30
  • public function execute(\Magento\Framework\Event\Observer $observer) { $request = $observer->getEvent()->getData('request'); if ($request->getModuleName() != 'catalog' || $request->getControllerName() !='product') { $controller = $observer->getControllerAction(); $this->_actionFlag->set('',\Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); $this->redirect->redirect($controller->getResponse(),'catalog/product/view/id/1'); } }
    – Bashir
    Feb 21, 2017 at 18:30
  • No i didn't delete var/generation ... why should i ?
    – Bashir
    Feb 21, 2017 at 18:32

2 Answers 2


All needed was just to delete the var/generation and recompile di again "php magento setup:di:compile"


After editing __construct function of any class you need to remove /var/generation directory.

Your Answer

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

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