2

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.

1
  • 2
    You shouldn't be calling your app controller from a behavior in the first place. If you really must have access to your controller, then use a component Commented Mar 3, 2011 at 7:36

2 Answers 2

12

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.)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

requestAction() is depreciated since CakePHP 3.4, please use other solutions such as View Cell. Following is the link to study View Cell [sanisoft.com/blog/2014/09/15/view-cell-cakephp-3-quick-start/]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.