6

when I do compile I do get the following error which is referencing my custom module Blocks, class

        Incorrect dependency in class ...
\Magento\Framework\Registry already exists in context object

my constructor looks like

public function __construct(
    \Magento\Framework\Registry $registry,
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
    \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
    array $data = []
) {
    $this->_collectionFactory = $collectionFactory;
    $this->_productsFactory = $productsFactory;
    $this->_coreRegistry = $registry;
    parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
}

what went wrong in this case?

5

It says that \Magento\Catalog\Block\Product\Context already has \Magento\Framework\Registry in it. So instead of injecting both of them, you should inject only Context and get registry from it via \Magento\Catalog\Block\Product\Context::getRegistry()

| improve this answer | |
6

Just upload your code same as below,

Keep $this->_coreRegistry = $context->getRegistry();

Full code,

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
    \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
    array $data = []
) {
    $this->_collectionFactory = $collectionFactory;
    $this->_productsFactory = $productsFactory;
    $this->_coreRegistry = $context->getRegistry();
    parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
}

Remove var folder and compile again. Registry is already called inside product block so its display compile errror in your case.

| improve this answer | |
  • When I use above method for resultFactory then getting error (RuntimeException): Source class "\Nitesh\ModuleName\Controller\Payment\Result" for "Nitesh\ModuleName\Controller\Payment\ResultFactory" generation does not exist. – Nitesh Feb 6 '17 at 14:48
  • You have to remove var folder from root and check – Rakesh Jesadiya Feb 6 '17 at 14:49
  • tried after removing var directory but still error comes – Nitesh Feb 6 '17 at 14:56

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.