Ketergantungan salah ScopeConfigInterface sudah ada di objek konteks dalam kompilasi magento2


9
<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Ortho\Featuredproduct\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
/**
 * Search helper
 */
class Data extends AbstractHelper
{


   /** * @var \Magento\Framework\App\Config\ScopeConfigInterfac */
    protected $_scopeConfig;
    protected $_config;
    protected $_storeManager;
    protected $_productFactory;
    CONST FEATURED_ENABLE = 'featured_settings/general/isenable';
    CONST FEATURED_TITLE = 'featured_settings/general/title';
    CONST FEATURED_LIMIT = 'featured_settings/general/limit';
    CONST FEATURED_SIDEENABLE = 'featured_settings/general/isleftenable';
    CONST FEATURED_SIDELIMIT = 'featured_settings/general/sidebarlimit';
    CONST FEATURED_METATITLE = 'featured_settings/featured_metadata/meta_title';
    CONST FEATURED_METAKEYWORD = 'featured_settings/featured_metadata/meta_keyword';
    CONST FEATURED_MTEADESC = 'featured_settings/featured_metadata/meta_description';


    /**
     * Initialize
     *
     * @param Magento\Framework\App\Helper\Context $context
     * @param Magento\Catalog\Model\ProductFactory $productFactory
     * @param Magento\Store\Model\StoreManagerInterface $storeManager
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context, 
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, 
        \Magento\Store\Model\StoreManagerInterface $storeManager, 
        array $data = []
    ) {
        $this->_productFactory = $productFactory;
        $this->_storeManager = $storeManager;
        $this->_scopeConfig = $scopeConfig;
        parent::__construct($context, $data);
    }

    public function getFeaturedstatus()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_ENABLE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    public function getFeaturedlimit()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_LIMIT,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    public function getFeaturedTitle()
    {
        ///echo 'check';
        return $this->_scopeConfig->getValue(self::FEATURED_TITLE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    public function getFeaturedleftstatus()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_SIDEENABLE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    public function getFeaturedleftlimit()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_SIDELIMIT,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }


    public function getMetaTitle()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_METATITLE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }


    public function getMetaKeyword()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_METAKEYWORD,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }


    public function getMetaDescription()
    {
        return $this->_scopeConfig->getValue(self::FEATURED_MTEADESC,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }



    public function getBreadcrumbs(\Magento\Framework\View\Result\Page $resultPage) {

        ///echo 'check breadcumbs';
        $breadcrumbs = $resultPage->getLayout()->getBlock('breadcrumbs');

        $breadcrumbs->addCrumb(
             'home', [
            'label' => __('Home'),
            'title' => __('Home Page'),
            'link' => $this->_storeManager->getStore()->getBaseUrl()
                ]
        );
        $breadcrumbs->addCrumb(
                'cms_page', ['label' => __('Featured Product'), 'title' => __('Featured Product')]
        );
    }
}

Jawaban:


17

Kesalahan Anda berasal dari fakta bahwa Anda menyuntikkan \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigdi konstruktor sedangkan kelas ini sudah menjadi bagian dari Magento\Framework\App\Helper\AbstractHelperkelas induk . Lihat yang berikut dari kelas induk:

protected $scopeConfig;

public function __construct(Context $context)
{
    $this->_moduleManager = $context->getModuleManager();
    $this->_logger = $context->getLogger();
    $this->_request = $context->getRequest();
    $this->_urlBuilder = $context->getUrlBuilder();
    $this->_httpHeader = $context->getHttpHeader();
    $this->_eventManager = $context->getEventManager();
    $this->_remoteAddress = $context->getRemoteAddress();
    $this->_cacheConfig = $context->getCacheConfig();
    $this->urlEncoder = $context->getUrlEncoder();
    $this->urlDecoder = $context->getUrlDecoder();
    $this->scopeConfig = $context->getScopeConfig();
}

Dengan demikian Anda tidak perlu menyuntikkan kelas itu Anda dapat menghapus baris berikut:

protected $_scopeConfig;

Dan perbarui konstruktor Anda seperti ini:

public function __construct(
    \Magento\Framework\App\Helper\Context $context, 
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager, 
    array $data = []
) {
    $this->_productFactory = $productFactory;
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}

Akhirnya, Anda dapat mengganti setiap panggilan berikut:

$this->_scopeConfig

Dengan:

$this->scopeConfig

Bekerja seperti pesona terima kasih banyak saya menghargai jawaban Anda.
user3921091

@ user3921091 Anda harus menandai jawaban ini. Karena ini juga bekerja untuk saya
Gujarat Santana
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.