Harsha M V
Harsha M V

Reputation: 54979

CakePHP: calling function in the AppController

How to call a function inside the app_controller.php in app/app_controller.php in the behavior of a plugin which is at app/plugins/media/models/behaviors/transfer.php inside a method called transferTo.

Upvotes: 2

Views: 10528

Answers (2)

Vinod Kumar
Vinod Kumar

Reputation: 451

You can use the requestAction() for this. The requestAction is a way to call any controller function from any different controller.

The syntax is

$response = $this->requestAction('name of controller/action_name/'.$parameter);

you will get the result in $response variable.

Upvotes: 0

deceze
deceze

Reputation: 522510

You don't. Models and/or behaviors should not talk back to the controller. If the method is so universally usable, make it a function in bootstrap.php, put it in AppModel if it's applicable there or create your own utility class in /app/libs that you can call statically from anywhere.

(You can call AppController::myMethod() anywhere, provided you're in a normal request cycle where the AppController is already loaded, or use ClassRegistry::init to get an instance of any controller (which will have the method), but this'll probably create more problems than it solves. Don't do this.)

Upvotes: 12

Related Questions