Why can't I inject the ProductRepositoryInterface in Magento 2?
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'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.
I was not able to inject any module to a controller with this error message. Deleting var/generation worked for me..