WHere to save a custom class and how to load it in a CakePHP Component?(在哪里保存自定义类以及如何在 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.
谢谢!
推荐答案
如果你写了自定义类,你把它放在 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 组件中加载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在哪里保存自定义类以及如何在 CakePHP 组件中加载它?
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
