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条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题