I get the following error when I try to start the Magento 2 module:
Fatal error: Uncaught TypeError: argument 1 passed to MyModule \ Service \ Controller \ Module \ Version :: __ construct () must be an instance of Magento \ Framework \ App \ Action \ Context, an instance of Magento \ Framework \ ObjectManager \ ObjectManager is called, / srv is called / www / vendor / magento / framework / ObjectManager / Factory / AbstractFactory.php on line 93 and /srv/www/app/code/MyModule/Service/Controller/Module/version.php:16
This happens after compiling this command:
magento setup:di:compile
I read a lot of posts that offer to clear the / var / di and / var / generation folders, and at the same time fixes an error that only works in the development environment. I cannot clear these folders in the production environment, as this will break other extensions.
This is my controller:
namespace MyModule\Service\Controller\Module;
class Version extends \MyModule\Service\Controller\Module {
protected $resultJsonFactory;
protected $objectManager;
protected $helper = null;
protected $config = null;
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();
}
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);
}
}
And this is what I have for 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;
}
}
source
share