To save a custom class and how to load it into CakePHP component?

I have a custom class called MathLib.php, and I need to use some login inside this class in all controllers. Reading CakePHP documentation I found that components are the best way to do this. But now I have a problem, I would like to know where I need to save the MathLib.php class (in which folder I should put the user class) and how to load it in the component.

Thanks!

+4
source share
1 answer

If you wrote a custom class, you will put it in app\libs for cake 1.x and in app\Lib for cake 2.x if it is not in the \ vendors or app \ Vendor application.

To load it into a component for cake 2.x, you must add before declaring the component class:

 App::uses('MathLib', 'Lib'); 

The class name and file name must be the same.

For 1.x you should download it:

 App::import('Lib', 'MathLib'); 

Further information for 1.x is here http://book.cakephp.org/1.3/view/1579/Library-classes

If this is a provider, the same idea, but read these documents: http://book.cakephp.org/1.3/view/944/Vendor-examples .

This is the file that called it important.

+10
source

Source: https://habr.com/ru/post/1414536/


All Articles