【发布时间】:2013-10-13 12:05:06
【问题描述】:
对于CakePHP 2.3.8如何在 CronController.php 中调用另一个控制器函数
有什么想法吗?
【问题讨论】:
-
猜组件是更好的选择?为什么需要在另一个控制器内部调用控制器?
标签: controller cakephp-2.0 cakephp-2.3 cron-task
对于CakePHP 2.3.8如何在 CronController.php 中调用另一个控制器函数
有什么想法吗?
【问题讨论】:
标签: controller cakephp-2.0 cakephp-2.3 cron-task
下面是代码:
App::import('Controller', 'Products'); // mention at top
// Instantiation // mention within cron function
$Products = new ProductsController;
// Call a method from
$Products->ControllerFunction();
希望对某人有所帮助!
【讨论】:
我参考了手册来找到解决方案。
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
【讨论】:
在您的控制器操作中使用$this->requestAction(); 方法。这不是最推荐的模式,但它很有用,可以根据您的参数返回数据或呈现视图。
【讨论】:
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));
}
【讨论】:
试试这个
<?php echo $this->Html->link( "Logout,".$user["username"], array('controller'=>'Users' ,'action'=>'logout') );?>
【讨论】: