2016-11-22 66 views
4

我得到Magento 2:傳遞給Controller :: __ construct()的參數1必須是.. .. .. Action Context的實例.. .. .. .. .. .. .. ObjectManager的實例給出

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 2模塊以下錯誤

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的\服務\控制器

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

我自己在這裏問了一個類似的問題:http://magento.stackexchange.com/q/144118/24432但到目前爲止我沒有解決方案。如果你找到一個,請告訴我。 – peedee

回答

0

嘗試刪除舊的生成使用RM -rf VAR /生成的文件/ *來自magento根目錄的命令,因爲magento預先生成具有構造函數的所有類文件。生成的類擴展了原始類,並由magento使用來調用插件。

相關問題