13

I have a controller action into which I would like to inject the product-repository

namespace Nosto\Tagging\Controller\Export;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Backend\App\Action;
use Magento\Framework\App\Action\Context;

class Test extends Action
{

    private $_productRepository;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        ProductRepositoryInterface $productRepository    
    ) {
        parent::__construct($context);
        $this->_storeManager = $storeManager;
        $this->_productRepository = $productRepository;
    }

No matter what order I put my constructor arguments, Magento's dependency-injection always injects an invalid class for the productRepository argument leading PHP to throw and argument error. The storeManager getd injected just fine. Clearing the caches haven't helped.

Here is the exception thrown:

Recoverable Error: Argument 3 passed to Nosto\Tagging\Controller\Export\Test::__construct() must implement interface Magento\Catalog\Api\ProductRepositoryInterface, instance of Nosto\Tagging\Helper\Account given.

The class names and argument positions in the error message change but the error definition is always the same. Removing the ProductRepositoryInterface from the constructor makes everything fine again.

3
  • Re: "Magento's dependency-injection always injects an invalid class for the productRepository argument" Including the exact error will help other people diagnose the problem. Commented Feb 1, 2016 at 8:42
  • 1
    did you clear var/generation?
    – Marius
    Commented Feb 1, 2016 at 8:46
  • @AlanStorm. Sorry about that. I've edited the question. Commented Feb 1, 2016 at 8:47

5 Answers 5

19

This generally crops up when you add another parameter to the constructor because Magento caches it in var/generation. You need to clear var/generation which forces Magento to regenerate the interceptor.

5
  • It seems that you were right. I've been clearing the caches from the admin and that didn't help. Commented Feb 3, 2016 at 6:49
  • 2
    Looks like in new installation, generated folder is in the root folder structure not in var, might help
    – xelber
    Commented May 13, 2017 at 21:42
  • I have created a custom module and trying this code inside Custom\Module\MagentoU\Test class; public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Model\ProductFactory $productFactory, \Magento\Checkout\Model\Session $session, \Custom\Module\Api\ProductRepositoryInterface $unit1ProductRepository, array $data, $justAParameter = "" ) <preference for="Custom\Module\Api\ProductRepositoryInterface" type="Magento\Catalog\Model\ProductRepository" /> Commented Nov 7, 2017 at 9:55
  • Tried everything still getting the same error Argument 4 passed to Custom\Module\MagentoU\Test::__construct() must be an instance of Custom\Module\Api\ProductRepositoryInterface, instance of Magento\Catalog\Model\ProductRepository\Interceptor given Commented Nov 7, 2017 at 9:58
  • Run php bin/magento setup:upgrade after it
    – Black
    Commented Jan 10, 2020 at 9:28
4

It's hard to say without further details, but I'm going to assume youe code sample actually looks something like this

namespace Packagename\Module\Controller;
class Test extends Action
{

    private $_productRepository;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        ProductRepositoryInterface $productRepository    
    ) {
        parent::__construct($context);
        $this->_storeManager = $storeManager;
        $this->_productRepository = $productRepository;
    }
}

i.e. with a PHP namespace (since all controllers have namespaces in Magento 2).

If that's the case, then in your code sample you're actually trying to inject a Packagename\Module\Controller\ProductRepositoryInterface class. If you use a class name without a namespace prefix, PHP assumes you want a class in the current namespace.

4

I was not able to inject any module to a controller with this error message. Deleting var/generation worked for me..

2

What worked for me is running the compile command.

/var/www/magento2-root$ php bin/magento setup:di:compile
0

In magento 2.2 delete folder:

"generated/code"

And that will solve the problem.

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.