CakePHP 2.3.8:在CronController.php中调用另一个Controller函数

Adi*_*att 8 controller cron-task cakephp-2.0 cakephp-2.3

对于CakePHP 2.3.8如何在CronController.php中调用Another Controller函数

有任何想法吗?

Adi*_*att 34

以下是代码:

App::import('Controller', 'Products'); // mention at top

// Instantiation // mention within cron function
$Products = new ProductsController;
// Call a method from
$Products->ControllerFunction();
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助!

  • 这绝对不推荐!根据代码的作用,使用[components](http://book.cakephp.org/2.0/en/controllers/components.html)(已提及@xialinZZZ),库或[models](http:// book.cakephp.org/2.0/en/models.html)而不是. (7认同)
  • 可能是,但至少我的工作暂时没有陷入困境,这似乎是寻找快速解决方案的简单方法 (2认同)

usu*_*oio 7

我参考了手册来找到解决方案。

public function that_controller_function_you_are_writing () {

    # this is cakes way of running required
    App::import('Controller', 'Users');
    $UsersController = new UsersController;

    # now you can reference your controller like any other PHP class
    $UsersController->that_function_you_needed();
}
Run Code Online (Sandbox Code Playgroud)

这是链接:http : //book.cakephp.org/2.0/en/core-utility-libraries/app.html


Der*_*rek 5

$this->requestAction();在控制器操作中使用该方法.它不是最推荐的模式,但它可能很有用,可以根据您的参数返回数据或渲染视图.