Magento 2 StoreManagerInterface đã tồn tại trong đối tượng bối cảnh trong quá trình biên dịch


15

Tôi nhận được lỗi này trong phần mở rộng của tôi.

GóiName \ ModuleName \ Block \ Nâng cao
Sự phụ thuộc không chính xác trong lớp GóiName \ ModuleName \ Block \ Nâng cao trong /var/www/html/app/code/PackageName/ModuleName/Block/Enhified.php \ Magento \ Store \ Model \ StoreManagerInterface đã tồn tại đối tượng bối cảnh

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

Câu trả lời:


12

Bạn không cần phải tiêm \Magento\Store\Model\StoreManagerInterfacevào hàm tạo của mình vì lớp cha đã làm điều đó.

Tôi giả sử khối của bạn mở rộng Magento\Framework\View\Element\Templateđã có mã sau đây:

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

Do đó, bạn có thể thay thế mã của mình bằng:

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 giây quá muộn.
— Marius

@Marius haha. Tôi vẫn thấy thú vị khi thấy hai người nói tiếng Anh không phải là người bản ngữ giải thích điều tương tự =)
— Raphael tại Digital Pianism

@Marius và Raphael +2. Nhanh quá
— Khoa TruongDinh

5

bạn không cần phải thêm \Magento\Store\Model\StoreManagerInterface $storeManagernhư một phụ thuộc vào lớp học của bạn.
Bạn đã có quyền truy cập vào một thực hiện StoreManagerInterfacetrong Magento\Framework\View\Element\Template\Contextlớp.
Xem này .

Vì vậy, bạn có thể làm cho hàm tạo của bạn trông như thế này:

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

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

Và bạn vẫn sẽ có thể truy cập storeManagerbiến thành viên như thế này $this->_storeManager.


5

Các phương thức sau đây có sẵn trong 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
)
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.