\Magento\Framework\Registry already exists in context object?

by Bhaskar C Pothineni   Last Updated August 10, 2018 12:09 PM

I wrote the following code for displaying the products (like widget.)

    use Magento\Catalog\Block\Product\AbstractProduct;
    use Magento\Catalog\Block\Product\Context;
    use Magento\Catalog\Model\Product\Visibility;
    use Magento\Framework\DataObject\IdentityInterface;
    use Magento\Framework\Module\Manager as ModuleManager;
    use Magento\Framework\Registry;
    use Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory;

    /**
       * Product Most Ordered Block
      */
       class ProductList extends AbstractProduct implements  IdentityInterface
     { 
        /**
          * Core Registry
          *
          * @var \Magento\Framework\Registry
         */
        protected $_coreRegistry;

/**
 * Product Collection
 * 
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
protected $_itemCollection;

/**
 * Catalog Product Visibility
 *
 * @var \Magento\Catalog\Model\Product\Visibility
 */
protected $_catalogProductVisibility;

/**
 * Module Manager
 * 
 * @var \Magento\Framework\Module\Manager
 */
protected $moduleManager;

/**
 * Reports Product Collection Factory
 * 
 * @var \Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory
 */
protected $_productsFactory;    

/**
 * Initialize Block
 *
 * @param Context $context
 * @param Visibility $catalogProductVisibility
 * @param ModuleManager $moduleManager
 * @param Registry $registry
 * @param CollectionFactory $productsFactory
 * @param array $data
 */
public function __construct(
    Context $context,
    Visibility $catalogProductVisibility,
    ModuleManager $moduleManager,
    Registry $registry,
    CollectionFactory $productsFactory,
    array $data = []
) {
    $this->_catalogProductVisibility = $catalogProductVisibility;
    $this->moduleManager = $moduleManager;
    $this->_coreRegistry = $registry;
    $this->_productsFactory = $productsFactory;

    parent::__construct(
        $context,
        $data
    );
}

When I do bin/magento setup:di:compile it is showing error like

Incorrect dependency in class Faonni\ProductMostOrdered\Block\ProductList in /var/www/html/New/magento-dev/app/code/Faonni/ProductMostOrdered/Block/ProductList.php \Magento\Framework\Registry already exists in the context object

Tags : magento-2.1


Answers 3


Remove Registry $registry from argument and

Replace below line

$this->_coreRegistry = $registry;

to

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

Suresh Chikani
Suresh Chikani
August 10, 2018 11:28 AM

There is no need to include Magento\Framework\Registry again in your class as you are extending Magento\Catalog\Block\Product\AbstractProduct and it already includes Magento\Framework\Registry.
Removing Magento\Framework\Registry from your class will solve your issue.

Anshu Mishra
Anshu Mishra
August 10, 2018 11:33 AM

Using the context registry should fix it :

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

Meaning instead of injecting both your registry and context's registry you can use the context registry directly

so your code should be like :

<?php
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\DataObject\IdentityInterface; 
use Magento\Framework\Module\Manager as ModuleManager;
use Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory;
/**
  * Product Most Ordered Block
*/
class ProductList extends AbstractProduct implements IdentityInterface {

 /**
      * Core Registry
      *
      * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;    

/**
 * Product Collection
 * 
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
protected $_itemCollection;

/**
 * Catalog Product Visibility
 *
 * @var \Magento\Catalog\Model\Product\Visibility
 */
protected $_catalogProductVisibility;

/**
 * Module Manager
 * 
 * @var \Magento\Framework\Module\Manager
 */
protected $moduleManager;

/**
 * Reports Product Collection Factory
 * 
 * @var \Faonni\ProductMostOrdered\Model\ResourceModel\Product\CollectionFactory
 */
protected $_productsFactory;

/**
 * Initialize Block
 *
 * @param Context $context
 * @param Visibility $catalogProductVisibility
 * @param ModuleManager $moduleManager
 * @param Registry $registry
 * @param CollectionFactory $productsFactory
 * @param array $data
 */
public function __construct(
        Context $context, 
        Visibility $catalogProductVisibility, 
        ModuleManager $moduleManager, 
        CollectionFactory $productsFactory, 
        array $data = []
) {
    $this->_catalogProductVisibility = $catalogProductVisibility;
    $this->moduleManager             = $moduleManager;
    $this->_coreRegistry             = $context->getRegistry();
    $this->_productsFactory          = $productsFactory;

    parent::__construct(
            $context, $data
    );
}
//...
}
Mohamed23gharbi
Mohamed23gharbi
August 10, 2018 11:37 AM

Related Questions



Magento 2 Checkouts page customization

Updated May 21, 2017 16:09 PM



Shopping cart price rule shipping issue

Updated May 16, 2018 16:09 PM