首页 文章

Magento 2:传递给Controller :: __ construct()的参数1必须是.. \ .. \ .. \ Action \ Context的实例,给出.. \ .. \ .. \ ObjectManager的实例

提问于
浏览
4

我尝试运行Magento 2模块时收到以下错误:

致命错误:未捕获TypeError:参数1传递给MyModule \ Service \ Controller \ Module \ Version :: __ construct()必须是Magento \ Framework \ App \ Action \ Context的实例,给出Magento \ Framework \ ObjectManager \ ObjectManager的实例,在第93行的/srv/www/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php中调用,并在/srv/www/app/code/MyModule/Service/Controller/Module/version.php:16中定义

在编译运行此命令后会发生这种情况:

magento setup:di:compile

我已经阅读了很多帖子,建议清除/ var / di和/ var / generation文件夹,然后修复错误,这只适用于开发环境 . 我无法在 生产环境 环境中清除这些文件夹,因为它会导致其他扩展中断 .

这是我的控制器:

namespace MyModule\Service\Controller\Module;

class Version extends \MyModule\Service\Controller\Module {

    protected $resultJsonFactory;
    protected $objectManager;
    protected $helper = null;
    protected $config = null;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
     * @param \MyModule\Service\Helper\Data $helper
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \MyModule\Service\Helper\Data $helper
    ) {

        $this->resultJsonFactory = $resultJsonFactory;
        $this->helper = $helper;
        $this->objectManager = $context->getObjectManager();
        parent::__construct($context);
        parent::initParams();

    }

    /**
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $result = $this->resultJsonFactory->create();
        $data = new \stdClass();
        $data->magentoVersion = (string) $this->objectManager->get('\Magento\Framework\App\ProductMetadata')->getVersion();
        $data->phpVersion = (string) phpversion();
        $data->moduleEnabled = $this->helper->getConfig()['enabled'];
        $data->apiVersion = "2.0";
        return $result->setData($data);
    }
}

这就是我对MyModule \ Service \ Controller的看法

namespace MyModule\Service\Controller;

abstract class Module extends \Magento\Framework\App\Action\Action {

    protected $pageSize = null;
    protected $pageNum = 0;
    protected $startDate = null;
    protected $endDate = null;
    protected $sortDir = 'asc';
    protected $filterField = 'created_at';
    protected $id = null;
    protected $helper;

    protected function initParams() {
        if ((bool) $pageSize = $this->getRequest()->getParam('page_size')) {
            $this->pageSize = $pageSize;
        }
        if ((bool) $pageNum = $this->getRequest()->getParam('page_num')) {
            $this->pageNum = $pageNum;
        }
        if ((bool) $startDate = $this->getRequest()->getParam('start_date')) {
            $this->startDate = $startDate;
            if ((bool) $endDate = $this->getRequest()->getParam('end_date')) {
                $this->endDate = $endDate;
            } else {
                $this->endDate = date('Y-m-d');
            }
        } elseif ((bool) $updatedStartDate = $this->getRequest()->getParam('updated_start_date')) {
            $this->filterField = 'updated_at';
            $this->startDate = $updatedStartDate;
            if ((bool) $updatedEndDate = $this->getRequest()->getParam('updated_end_date')) {
                $this->endDate = $updatedEndDate;
            } else {
                $this->endDate = date('Y-m-d');
            }
        }
        if ((bool) $sortDir = $this->getRequest()->getParam('sort_dir')) {
            $this->sortDir = $sortDir;
        }
        if ((bool) $id = $this->getRequest()->getParam('id')) {
            $this->id = $id;
        }
    }

    protected function isEnabled() {
        return $this->helper->getConfig()['enabled'];
    }

    protected function isAuthorized() {

        $token = $this->helper->getConfig()['security_token'];
        $authToken = (isset($_SERVER['HTTP_X_TOKEN']) ? $_SERVER['HTTP_X_TOKEN'] : $_SERVER['X_TOKEN']);

        if (empty($authToken)) {
            return false;
        }

        if (trim($token) != trim($authToken)) {
            $this->helper->log('feed request with invalid security token');
            return false;
        }

        return true;
    }
}

2 回答

  • 0

    尝试使用magento根目录中的rm -rf var / generation / *命令删除旧生成的文件,因为magento使用其构造函数预生成所有类文件 . 生成的类扩展了原始类,并由magento用来调用插件 .

  • 0

    如果您处于 生产环境 模式,请更改为维护模式,清除var / cache,var / generation并重新运行compile . 并且不要忘记禁用维护模式

相关问题