Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place a custom PHP class in CakePHP 3?

I have a new PHP class which I would like to call from a Controller. Where, in the CakePHP folder structure, should I place this new class and what is the procedure to invoke or make use of it from a controller? Thanks in advance for your cooperation!

like image 758
Jorge Avatar asked Dec 08 '15 03:12

Jorge


1 Answers

From my point of view, you can reuse any own class and also any third parties class as a utility class. If so, then you can place the class into src/Utility folder. Please use the proper namespace. After that, you can use that class anywhere in CakPHP 3.x.

HOW TO PLACE:

Say, you have a class named Jorge, save it into src/Utility folder using file name Jorge.php. Put namespace App\Utility; statement top of your Jorge.php file.

HOW TO USE:

In the file where you want to use this class, just put use App\Utility\Jorge;. After that, you can call the class into that file.

ALTERNATIVE SOLUTION:

If you have a third party package of many classes, then you can follow https://stackoverflow.com/a/28527683/1787600

like image 121
monsur.hoq Avatar answered Sep 21 '22 14:09

monsur.hoq