Cakephp 2.5 call one controller method inside another controller

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.

Try to use oop,

use(ā€˜AControllerā€™,'Controller);
class BController ā€¦
function bb(){
$a = new AController();
ā€¦
$a->aa();
ā€¦
}

1 Like

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

1 Like

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

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

1 Like