10

对于CakePHP 2.3.8如何在 CronController.php 中调用另一个控制器函数

有任何想法吗?

4

5 回答 5

37

下面是代码:

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

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

希望它可以帮助一些人!

于 2013-10-13T12:48:19.097 回答
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();
}

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

于 2014-01-07T21:51:50.063 回答
4

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

于 2013-10-17T05:19:04.963 回答
4

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));
     }
于 2016-07-11T00:33:22.863 回答
-3

试试这个

  <?php echo $this->Html->link( "Logout,".$user["username"],   array('controller'=>'Users' ,'action'=>'logout') );?>
于 2014-05-14T14:51:24.413 回答