Re-Use controller code in Command (cakephp5)


I am making a function in my controller that imports a lot of data from an API. The code is working and is clear, but I also want a console command to start this import. I can copy-paste the code, but when I do that I have to edit the code twice when I am going to make some changes.

I saw on google that there is an Utility class in older versions of cakephp but I can’t find it for cakephp5. How can I make it possible without copy-pasting the code?

CakePHP 3, 4 and 5 are all built as “modern PHP apps” with composer and therefore support PHP namepsaces.

If you don’t know what that is see my video from 2 years ago. Everything in there still applies and is totally valid right now with CakePHP 5.

1 Like