对于CakePHP 2.3.8
如何在CronController.php中调用另一个控制器函数
有什么想法吗?
答案 0 :(得分:33)
以下是代码:
App::import('Controller', 'Products'); // mention at top
// Instantiation // mention within cron function
$Products = new ProductsController;
// Call a method from
$Products->ControllerFunction();
希望它有所帮助!
答案 1 :(得分:5)
在控制器操作中使用$this->requestAction();
方法。它不是最推荐的模式,但它可能很有用,可以根据您的参数返回数据或渲染视图。
答案 2 :(得分:3)
我参考了手册以找到解决方案。
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();
}
这是链接: http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
答案 3 :(得分:3)
App::import('Controller', 'XXX');
对我不起作用。
我正在使用Cake 3.0
过了一段时间我才开始工作
您想要呼叫的控制器的功能:
public function validateSomething($var = null)
{
return ...
}
在另一个控制器中,您需要调用上一个函数来验证某些内容:
public function index()
{
// load the model you need depending on the controller you need to use
$this->loadModel('User');
// use this in case you have tu instantiate a new entity
$user = $this->User->newEntity();
$user = $this->User->patchEntity($user, $this->request->data);
// using the controller on the fly, you could assign it to a var
// call the function you need
$result = (new UserController())->validateSomething($user);
// Test if result has something:
$this->Flash->success(__($result));
}
答案 4 :(得分:-2)
试试这个
<?php echo $this->Html->link( "Logout,".$user["username"], array('controller'=>'Users' ,'action'=>'logout') );?>