Call on controller from another


I’m building my first CakePHP 5 app and have run into what I’m sure is a very simple problem.

In Cake 4 I often call a method in one controller from another - I will typically set up a utility controller for various formatting tasks etc and then call it from other controllers.

To do this I just used (new UtilitiesController())->aMethod($args) which worked perfectly.

In Cake 5 I can’t get this to work, looking at the error message I got when I first tried to use this it seems I need to pass the request in as an argument, so I tried

(new UtilitiesController($this->request))->aMethod($args)

Which seemed to access the third party controller OK, except the function did not work correctly, and when I tested it by calling the same function directly from the view layer it worked fine, so I think there is something wrong with how I am trying to access the 3rd party method.



Controllers are not meant to be created by you manually.

What you are looking for is called a utility/service class to share logic between your controllers or other parts of your app.


In combination with Dependency Injection you can inject your service class into any controller action and re-use that logic

I also showcase this in my latest workshop