从另一个控制器调用时,Component和beforeFilter未加载到控制器中

时间:2013-11-21 15:35:58

标签: cakephp controller components

我在controller1中调用controller2来执行某些操作。问题是,controller2的动作需要调用它的beforeFilter和一个在AppController中加载的Component。

实际上,在直接调用时,controller2的动作执行得很好,但在controller1中调用时会出现错误,如下所示:

App::import('Controller', 'Second');
$Second = new SecondController;
$Second->someAction();

在Component和beforeFilter中定义的变量显示为未设置。 如何通过调用beforeFilter并使用我的自定义组件来加载外部控制器?

1 个答案:

答案 0 :(得分:1)

在这种情况下,选项很少

1. Use request Actions http://book.cakephp.org/2.0/en/controllers.html#Controller::requestAction

2. Move the logic or code you want to execute from SecondCrontroller to common component and use it in both contrllers. 

3. Move the logic or code you want to execute from SecondCrontroller to AppController