首页 文章

Magento 2:当我在\ Magento \ Contact \ Controller \ Index \ Post类上使用它时出现ScopeConfig错误

提问于
浏览
2

首先,我正在扩展模块以便联系我们,我想检查后端是否启用了 ScopeConfig 但是我遇到了这个问题 . 我已经尝试删除生成的文件并运行setup upgrade命令 .

根据我的假设,我认为我的代码缺少一些东西

码:

<?php
namespace SCI\Form\Controller\Index;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Post extends \Magento\Contact\Controller\Index\Post
{
    protected $scopeConfig;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        ScopeConfigInterface $scopeConfig,
        array $data = []
    ){
        parent::__construct($context,$data);
        $this->_scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;

    }

    public function ifEnabled(){
        return $this->_scopeConfig->getValue('extended_contact/config/extended_contact_enabled',ScopeInterface::SCOPE_STORE);
    }

    public function execute()
    {
        die($this->ifEnabled());

        return parent::execute();

    }
}

注意:当我将类形式\ Magento \ Contact \ Controller \ Index \ Post更改为\ Magento \ Framework \ App \ Action \ Action时,它正在工作

错误:

致命错误:未捕获TypeError:参数2传递给Magento \ Contact \ Controller \ Index \ Post :: __ construct()必须实现接口Magento \ Contact \ Model \ ConfigInterface,给定数组,在C:\ xampp \ htdocs \ training \中调用第17行的app \ code \ SCI \ Form \ Controller \ Index \ Post.php,在C:\ xampp \ htdocs \ training \ vendor \ magento \ module-contact \ Controller \ Index \ Post.php中定义:49堆栈跟踪: #0 C:\ xampp \ htdocs \ training \ app \ code \ SCI \ Form \ Controller \ Index \ Post.php(17):Magento \ Contact \ Controller \ Index \ Post - > __ construct(Object(Magento \ Framework \ App) \ Action \ Context),Array)#1 C:\ xampp \ htdocs \ training \ generated \ code \ SCI \ Form \ Controller \ Index \ Post \ Interceptor.php(14):SCI \ Form \ Controller \ Index \ Post- > __ construct(Object(Magento \ Framework \ App \ Action \ Context),Object(Magento \ Store \ Model \ StoreManager),Object(Magento \ Framework \ App \ Config),Array)#2 C:\ xampp \ htdocs \ training \ vendor \ magento \ framework \ ObjectManager \ Factory \ AbstractFactory.php(111):SCI \ Form \ Controller \ Index \ Post \ Interceptor - > __ construct(Ob) ject(第49行的C:\ xampp \ htdocs \ training \ vendor \ magento \ module-contact \ Controller \ Index \ Post.php中的Magento \ Framework \ App \ Action \ Conte)

1 回答

  • 0

    您可以尝试将函数更改为以下代码以修复错误 .

    public function ifEnabled(){
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
    
        return $this->_scopeConfig->getValue(
            'extended_contact/config/extended_contact_enabled',
            $storeScope
        );
    }
    

相关问题