调用另一个控制器的动作

sha*_*yan 3 yii

我有一个网格视图,我想从另一个动作控制器获取列的值.现在我在控制器1中有这个

   array(
        'name'=>'title',
        'value'=>array($this,'Action2'),
    ),
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

controller1 and its behaviors do not have a method or closure named "Action2".
Run Code Online (Sandbox Code Playgroud)

如果我用"controller2"替换$ this

   array(
        'name'=>'title',
        'value'=>array('controller2','Action2'),
    ),
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

call_user_func_array() [<a href='function.call-user-func-array'>function.call-user-func-array</a>]: First argument is expected to be a valid callback, 'controller2::action2' was given
Run Code Online (Sandbox Code Playgroud)

也许这是不好的做法,但这是可行的吗?

rin*_*.io 5

以这种方式使用控制器动作是不好的做法.最好将代码放在模型的方法中.但如果您仍想这样做,这里有一种方法:

'value' => function() {
    list($controller) = Yii::app()->createController('controllerId');
    return $controller->actionTest();
}
Run Code Online (Sandbox Code Playgroud)

这是另一个:

'value' => function() {
    $controller = new TestController('test');
    return $controller->actionTest();
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用以下解决方案:

Yii::app()->runController('category/view/id/1');
Run Code Online (Sandbox Code Playgroud)