对于CakePHP 2.3.8
如何在 CronController.php 中调用另一个控制器函数
有任何想法吗?
对于CakePHP 2.3.8
如何在 CronController.php 中调用另一个控制器函数
有任何想法吗?
下面是代码:
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');
我不起作用。
我正在使用蛋糕 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') );?>