doujishao8793 2012-12-19 07:23
浏览 100
已采纳

CakePHP,在另一个控制器内导入一个控制器

I need to use a controller functions inside my another controller. But I don't want to make

$Myother = new MyotherController();

every time I use a method from that controller. How can I initialize a controller so I won't make a "new" in every method ?

I found this, It writes like this:

App::import('Controller', 'Pages');
class UsersController extends AppController {
  var $Pages;
  function beforeFilter() {
    $this->Pages =& new PagesController; /*Loads the class*/
    $this->Pages->constructClasses(); /*Loads the model associations, components, etc. of the    Pages controller*/
  }
  function index() {
    $this->Pages->index();
  }
}

Is this the proper way, or is there a better way ?

  • 写回答

2条回答 默认 最新

  • doudou7361 2012-12-19 09:33
    关注

    I think really the only way to go is to switch to using some components. Initializing new controllers with their components / helpers / models, will cause much more over head, which might not be worth the trade off. Let alone, It'll promote bad design.

    My advice, is to go with components.

    You can always use requestAction() if you need just the result of an action in another controller. However, even CakePHP say people should be careful with it.

    "If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model."

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?