Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHere to save a custom class and how to load it in a CakePHP Component?

I have a custom class named MathLib.php and I need to use some login inside this class in all the controllers. Reading CakePHP documentations I found that components are the best way to do this. But Now, I have a problem, I would like to know where do I have to save the MathLib.php class (in what Folder do i have to put custom class), and How can I load it in a component.

Thank you!

like image 906
Dail Avatar asked Dec 27 '22 00:12

Dail


1 Answers

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

To load it in a component for cake 2.x you would add before your component class declaration:

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

The class name and file name should be the same.

For 1.x you would load it by:

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

More info for 1.x here http://book.cakephp.org/1.3/view/1579/Library-classes

If it's a vendor, same idea, but read these docs: http://book.cakephp.org/1.3/view/944/Vendor-examples.

It's the file naming that's important.

like image 75
tigrang Avatar answered Jan 14 '23 15:01

tigrang