简体   繁体   English

从另一个控制器调用控制器功能

[英]call a controller function from another controller

I have 2 controllers , let's call them c1 and c2. 我有2个controllers ,我们称它们为c1和c2。 Now I want to call a function in c2, Let's say actionC2 , from a function in c1. 现在我想从c1中的函数调用c2中的函数,比方说actionC2

I tried something like this: 我尝试过这样的事情:
$c2_instance = new c2();
$c2_instance->actionC2();

but it won't work. 但它行不通。 I get this error: Missing argument 1 for CController::__construct() . 我收到此错误: Missing argument 1 for CController::__construct()
What am I doing wrong? 我究竟做错了什么?

EDIT: maybe its important to say that it falls on the first line 编辑:说它落在第一行也许很重要

You should not call a controller from another controller . 您不应从另一个控制器调用一个控制器。 You should redirect using this 您应该使用此重定向

$this->redirect(array('controller/action'));

And if you do not have exactly no way other that than , reconsider your design . 如果您除了没有别的选择,请重新考虑您的设计 Solve the problem , do not try to hide it. 解决问题,不要试图隐藏它。 It will bite back you anyway. 无论如何它都会咬你。

As @user488074 said, your controller must have an argument that it is looking for when you create an instance of it. 正如@ user488074所说,您的控制器必须具有在创建它的实例时正在寻找的参数。 Go to that controller and look at what it is looking for in the contstruct function. 转到该控制器,并在contstruct函数中查看它要查找的内容。 If you don't want to pass an argument all the time for this controller then add something like this to the construct function argument 如果您不想一直为该控制器传递参数,则将类似这样的内容添加到构造函数参数中

public function foo($argument = NULL){
}

so it has a default value if you don't want to pass something. 因此,如果您不想传递某些内容,则它具有默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM