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

北城以北 提交于 2019-12-22 09:26:58

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/10768816/where-to-save-a-custom-class-and-how-to-load-it-in-a-cakephp-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!