首页>Magento>source

我在扩展程序中收到此错误。

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;      
}
最新回答
  • 2019-8-16
    1 #

    你不需要注入 \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;
    }
    

  • 2019-8-16
    2 #

    你不需要添加 \Magento\Store\Model\StoreManagerInterface $storeManager   作为对你班级的依赖。
    您已经可以访问 StoreManagerInterface的一个实体   在 Magento\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 .

  • 2019-8-16
    3 #

    Context提供以下方法   对象( \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
    )
    

  • magento 1.9:如何控制模板在页面上的显示位置?
  • 根据付款方式发送特定电子邮件(即包括银行明细)