Magento 2 StoreManagerInterface已存在于编译中的上下文对象中


15

我的扩展程序中出现此错误。

PackageName \ ModuleName \ Block \ Enhanced
在类中的PackageName \ ModuleName \ Block \ Enhanced在/var/www/html/app/code/PackageName/ModuleName/Block/Enhanced.php \ Magento \ Store \ Model \ StoreManagerInterface中已经存在上下文对象

 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;      
}

Answers:


12

您不需要注入\Magento\Store\Model\StoreManagerInterface构造函数,因为父类已经做到了。

我假设您Magento\Framework\View\Element\Template的代码块扩展已经具有以下代码:

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);
}

因此,您可以将代码替换为:

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
啊... 13秒为时已晚。
马吕斯

@Marius哈哈。我仍然觉得很有趣,看看两位非英语母语者如何解释同一件事=)
拉斐尔在Digital Pianism

@Marius和Raphael +2。这么快。
Khoa TruongDinh'9

5

您不需要将其添加\Magento\Store\Model\StoreManagerInterface $storeManager为类的依赖项。
您已经可以StoreManagerInterfaceMagento\Framework\View\Element\Template\Context课程中访问的补充。
看到这个

因此,您可以使构造函数如下所示:

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

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

而且您仍然可以storeManager像这样访问成员变量$this->_storeManager


5

Contextobject(\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
)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.