Cakephp 2.5 call one controller method inside another controller


#1

Hi,

Can i call a controller method (A) inside another controller (B) ?

The only way i know is using a redirect, i am searching for a way to call without using redirect.


#2

Try to use oop,

use(‘AController’,'Controller);
class BController …
function bb(){
$a = new AController();

$a->aa();

}


#3

It’s not really good practice to do this. You can, as you said, use a redirect to pass control of your application to another Controller method altogether. However, if you don’t want to do that, you can factor the logic that you need into a Model method which can then be shared from any controller. Remember that all business logic should really be in your models anyway. Controllers simply respond to and field incoming requests (sort of like a traffic cop).

If your logic isn’t business-related, you can always factor your method into a Component which can then be shared with any other Controller in your app.

Component Docs
https://book.cakephp.org/2.0/en/controllers/components.html

The “Fat Model” Concept
https://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html

HTH


#4

I will use a componet. Is a shared logic i will use into two controllers. Thank you !


#5

in 3.0 I use $this->action_name() (method) and it works (in same controller).