12

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.

  • 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. – Alan Storm Feb 1 '16 at 8:42
  • 1
    did you clear var/generation? – Marius Feb 1 '16 at 8:46
  • @AlanStorm. Sorry about that. I've edited the question. – Mridang Agarwalla Feb 1 '16 at 8:47
17

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.

  • It seems that you were right. I've been clearing the caches from the admin and that didn't help. – Mridang Agarwalla Feb 3 '16 at 6:49
  • 1
    Looks like in new installation, generated folder is in the root folder structure not in var, might help – xelber May 13 '17 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" /> – Ashwani Shukla Nov 7 '17 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 – Ashwani Shukla Nov 7 '17 at 9:58
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, privacy policy and cookie policy

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