10

For CakePHP 2.3.8 How can I call Another Controller function in CronController.php

Any ideas?

Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104
  • 4
    guess component is a better alternative? Why do you need to call controller inside another controller? – xialin Oct 13 '13 at 12:12

5 Answers5

40

Below is the code:

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

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

Hope it helps some one !

Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104
  • 7
    This is absolutely not recommended! Depending on what your code does, use [components](http://book.cakephp.org/2.0/en/controllers/components.html) (as @xialinZZZ already mentioned), libraries or [models](http://book.cakephp.org/2.0/en/models.html) instead. – ndm Oct 13 '13 at 20:48
  • 2
    May be, but atleast my work is not stuck at moment, it seems pretty easy way go around for a quick solution finder – Aditya P Bhatt Oct 14 '13 at 10:38
  • The whole concept of a "cron controller" is *completely* flawed by design. You should use a shell and your data processing logic should be in a model so it can be shared between the controller and shell if needed. I down voted this answer as well because it is one of the most bad things you can do. – floriank Nov 06 '14 at 11:47
7

I referenced the manual to find a solution to this.

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();
}

This is the link: http://book.cakephp.org/2.0/en/core-utility-libraries/app.html

usumoio
  • 3,500
  • 6
  • 31
  • 57
4

Use the $this->requestAction(); method in your controller action. It's not the most recommended pattern, but it can be useful and can return data or render a view based on your parameters.

Derek
  • 4,575
  • 3
  • 22
  • 36
4

The App::import('Controller', 'XXX'); did not work for me.

I'm using Cake 3.0

After a while I made it work

Function of the controller you want to call:

    public function validateSomething($var = null)
    {
         return ...
    }

In a different controller, where you need to call the previous function to validate something:

 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));
     }
Kevin
  • 41,694
  • 12
  • 53
  • 70
lele
  • 442
  • 6
  • 14
-3

try this

  <?php echo $this->Html->link( "Logout,".$user["username"],   array('controller'=>'Users' ,'action'=>'logout') );?>
user3470042
  • 73
  • 3
  • 6