Dail
Dail

Reputation: 4608

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!

Upvotes: 4

Views: 5224

Answers (1)

tigrang
tigrang

Reputation: 6767

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.

Upvotes: 10

Related Questions