15

I am getting this error in my extension.

PackageName\ModuleName\Block\Enhanced
Incorrect dependency in class PackageName\ModuleName\Block\Enhanced in /var/www/html/app/code/PackageName/ModuleName/Block/Enhanced.php \Magento\Store\Model\StoreManagerInterface already exists in context object

 public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\Session $catalogSession,
    \Magento\Store\Model\StoreManagerInterface $storeManager,        
    array $data = []

)
{
    parent::__construct($context, $data);
    $this->_catalogSession = $catalogSession;
    $this->_storeManager = $storeManager;      
}

3 Answers 3

13

You don't need to inject \Magento\Store\Model\StoreManagerInterface in your constructor because the parent class already does that.

I assume your block extends Magento\Framework\View\Element\Template which already has the following code:

protected $_storeManager;

public function __construct(Template\Context $context, array $data = [])
{
    $this->validator = $context->getValidator();
    $this->resolver = $context->getResolver();
    $this->_filesystem = $context->getFilesystem();
    $this->templateEnginePool = $context->getEnginePool();
    $this->_storeManager = $context->getStoreManager();
    $this->_appState = $context->getAppState();
    $this->templateContext = $this;
    $this->pageConfig = $context->getPageConfig();
    parent::__construct($context, $data);
}

Thus you can replace your code with:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\Session $catalogSession,   
    array $data = []

)
{
    parent::__construct($context, $data);
    $this->_catalogSession = $catalogSession;
}
3
  • 3
    Ah...13 seconds too late.
    – Marius
    Commented Sep 14, 2016 at 7:25
  • @Marius haha. I still find interesting to see how two non native english speakers explain the same thing =) Commented Sep 14, 2016 at 7:26
  • @Marius and Raphael +2. So quickly. Commented Sep 14, 2016 at 8:11
5

you don't need to add \Magento\Store\Model\StoreManagerInterface $storeManager as a dependency to your class.
You already have access to an inplementation of StoreManagerInterface in the Magento\Framework\View\Element\Template\Context class.
See this.

So you can make your constructor look like this:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\Session $catalogSession,
    array $data = []

)
{
    parent::__construct($context, $data);
    $this->_catalogSession = $catalogSession;
}

And you will still be able to access storeManager member variable like this $this->_storeManager.

5

Following methods are available in Context object(\Magento\Framework\View\Element\Template\Context)

print_r(get_class_methods($context))

Array
(
    [0] => __construct
    [1] => getResolver
    [2] => getValidator
    [3] => getFilesystem
    [4] => getLogger
    [5] => getViewFileSystem
    [6] => getEnginePool
    [7] => getAppState
    [8] => getStoreManager
    [9] => getPageConfig
    [10] => getCache
    [11] => getDesignPackage
    [12] => getEventManager
    [13] => getLayout
    [14] => getRequest
    [15] => getSession
    [16] => getSidResolver
    [17] => getScopeConfig
    [18] => getInlineTranslation
    [19] => getUrlBuilder
    [20] => getAssetRepository
    [21] => getViewConfig
    [22] => getCacheState
    [23] => getEscaper
    [24] => getFilterManager
    [25] => getLocaleDate
)
0

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.