当我尝试运行我的 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/
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案
我有一个rubyonrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些