CakePHP 2.3.8: Calling Another Controller function in CronController.php
Asked Answered
H

5

10

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

Any ideas?

Hoffmann answered 13/10, 2013 at 12:5 Comment(1)
guess component is a better alternative? Why do you need to call controller inside another controller?Banuelos
H
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 !

Hoffmann answered 13/10, 2013 at 12:48 Comment(3)
This is absolutely not recommended! Depending on what your code does, use components (as @xialinZZZ already mentioned), libraries or models instead.Permeable
May be, but atleast my work is not stuck at moment, it seems pretty easy way go around for a quick solution finderHoffmann
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.Popery
C
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

Cara answered 7/1, 2014 at 21:51 Comment(0)
M
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.

Merola answered 17/10, 2013 at 5:19 Comment(2)
Why is it not recommended?Warmonger
Please explain why this is not recommended?Mitchellmitchem
C
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));
     }
Cosme answered 11/7, 2016 at 0:33 Comment(0)
Y
-3

try this

  <?php echo $this->Html->link( "Logout,".$user["username"],   array('controller'=>'Users' ,'action'=>'logout') );?>
Yare answered 14/5, 2014 at 14:51 Comment(2)
Can you please add some description of why you think this should work.Parlor
this is not related to the question.Ramentum

© 2022 - 2024 — McMap. All rights reserved.