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

报告相同问题?