保存自定义类以及如何将它加载到CakePHP组件中? [英] WHere to save a custom class and how to load it in a CakePHP Component?

查看:239
本文介绍了保存自定义类以及如何将它加载到CakePHP组件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为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.

谢谢!

推荐答案

自定义类,将它放在 app\libs 中用于蛋糕1.x和 app\Lib 中蛋糕2.x,如果没有,它会进入app\vendors或app\Vendor。

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.

要将它加载到蛋糕2.x的组件中,在你的组件类声明之前:

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.

对于1.x,您将加载它:

For 1.x you would load it by:

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

有关1.x的更多信息http://book.cakephp.org/1.3/view/1579/Library-classes

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组件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆