2

Just when I think I grasp the Magento 2 object manager and dependency injection something happens which I don't understand. I think I am passing context en pagefactory objects but Magento says I am not. The second one seems wrong.

Recoverable Error: Argument 2 passed to Demo\Hello\Controller\Adminhtml\Order\MassPrint::__construct() must be an instance of Magento\Framework\View\Result\PageFactory, instance of Magento\Framework\App\ResourceConnection given, called in 

The controller:

<?php 
namespace Demo\Hello\Controller\Adminhtml\Order;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class MassPrint extends \Magento\Backend\App\Action {

  public function __construct(Context $context, PageFactory $pageFactory) {
    parent::__construct($context);
  }

}

It understands the type hinting like I want:

must be an instance of Magento\Framework\View\Result\PageFactory

But it is passing

instance of Magento\Framework\App\ResourceConnection given

Why is this? There is some magic going on with Magento which I don't understand (yet).

  • 1
    clear var/registration. also loop for places where your class is used and see how it is instantiated. – Marius Feb 11 '16 at 22:52
  • Yup, seems like a var/cache or var/generation problem. Thanks! – Akif Feb 11 '16 at 23:00
  • I just did some testing and found out why this is happening. It seems that magento is making in interceptor class file in ´var/generation´. I would like to answer my own question with details because I think it´s important for developers who stuggle with the same problem to understand whats happening under the hood. – Akif Feb 12 '16 at 16:01
  • I reopened it. you can add your answer. – Marius Feb 12 '16 at 16:29
4

TL;DR

If you see a an error with Interceptor.php be sure to empty /var/generation/ before you look further. It will save you lots of time.

Explanation

After some testing I found the underlying cause. When executing a custom controller for the first time Magento automatically generates files. So far so good. In my case just one. It's living in var\generation\ The full path in this example would be var\generation\Demo\Hello\Controller\Adminhtml\Order\MassPrint\Interceptor.php.

This class is extending your custom controller. So naturally it is calling the parent constuctor in its own constuctor. So if you make changes to your controller constructor this file must be regenerated to mach your new code. If you don't it will yield unwanted results. So be sure to at least delete the Interceptor class for that controller. Easier would be to empty the whole director so Magento will regenerate all files matching your new code.

rm -rf var/generation/*

Actually magento gives a very good clue where to look in the last part of the error message.

called in /var/generation/Demo/Hello/Controller/Adminhtml/Order/MassPrint/Interceptor.php on line 15
  • I got a similar error like Call to undefined method Vendor\Module\Controller\Result\Index\Interceptor::__(), but after clearing var/gen still it persists, any idea where the root cause is? – Sushivam Aug 11 '16 at 10:49

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.