问题描述
我有一个名为 MathLib.php 的自定义类,我需要在所有控制器的这个类中使用一些登录.阅读 CakePHP 文档我发现组件是最好的方法.但是现在,我有一个问题,我想知道在哪里我必须保存 MathLib.php 类(我必须在什么文件夹中放置自定义类),以及如何加载它在一个组件中.
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.
谢谢!
推荐答案
如果你写了自定义类,你把它放在 applibs
for cake 1.x 和 appcake 2.x 的 Lib
,如果没有,它会进入 appvendors 或 appVendor.
If you wrote the custom class, you put it in applibs
for cake 1.x and in appLib
for cake 2.x, if not it goes inside the appvendors or appVendor.
要将其加载到 cake 2.x 的组件中,您需要在组件类声明之前添加:
To load it in a component for cake 2.x you would add before your component class declaration:
App::uses('MathLib', 'Lib');
类名和文件名要一致.
对于 1.x,您可以通过以下方式加载它:
For 1.x you would load it by:
App::import('Lib', 'MathLib');
这里有更多关于 1.x 的信息 http://book.cakephp.org/1.3/view/1579/库类
More info for 1.x here http://book.cakephp.org/1.3/view/1579/Library-classes
如果是供应商,同样的想法,但请阅读以下文档:http://book.cakephp.org/1.3/view/944/Vendor-examples.
If it's a vendor, same idea, but read these docs: http://book.cakephp.org/1.3/view/944/Vendor-examples.
重要的是文件命名.
这篇关于在哪里保存自定义类以及如何在 CakePHP 组件中加载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!