php - Magento 2:传递给 Controller::__construct() 的参数 1 必须是 ..\..\..\Action\Context 的实例,给定的 ..\..\..\ObjectManager 的实例

标签 php dependency-injection magento2

当我尝试运行我的 Magento 2 模块时出现以下错误:

Fatal error: Uncaught TypeError: Argument 1 passed to MyModule\Service\Controller\Module\Version::__construct() must be an instance of Magento\Framework\App\Action\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /srv/www/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /srv/www/app/code/MyModule/Service/Controller/Module/version.php:16

这发生在我编译运行这个命令之后:

magento setup:di:compile

我读过很多建议清除/var/di 和/var/generation 文件夹的帖子,虽然这修复了错误,但只适用于开发环境。我无法在生产环境中清除这些文件夹,因为这会导致其他扩展程序中断。

这是我的 Controller :

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

最佳答案

如果您处于生产模式,请切换到维护模式,清除 var/cache、var/generation 并重新运行编译。 并且不要忘记禁用维护模式

关于php - Magento 2:传递给 Controller::__construct() 的参数 1 必须是 ..\..\..\Action\Context 的实例,给定的 ..\..\..\ObjectManager 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40752470/

相关文章:

javascript - 脚本中所需的代码不起作用

php - 如何在magento中创建属性

php - 如何从php中的字符串中删除括号?

dependency-injection - Gradle 无法使用 fileTree 将依赖项注入(inject)子项目

javascript - Knockout 'data-bind' 中的 'scope' 关键字

PHP preg_match() 返回最后一次匹配的位置

python - 如何在 python 中对工厂进行单元测试?

java - 将依赖项注入(inject)静态字段

php - Magento 2 - 设置 :di:compile 中不存在类

php - magento2设置: no CSS