问题描述
我正在用 CakePHP 2.1 编写一个小应用程序,我想使用 Nick Baker 的文件上传插件.我下载了 cakephp2.0 分支(我知道还没有完成),并将它放在我的应用程序插件文件夹中.我对其进行了一些必要的修改,但我很好奇替换那些 App::import
函数调用的正确方法是什么(在 FileUploadComponent
的开头,FileUploadBehavior
和 FileUploadHelper
类)与 App:uses
函数.
I'm writing a small application in CakePHP 2.1, and I want to use Nick Baker's file upload plugin. I downloaded the cakephp2.0 branch (I know that isn't done yet), and placed it in my apps Plugin folder. I made some necessary modifications on it, but I'm curious what the right way is to replace those App::import
function calls (at start of FileUploadComponent
, FileUploadBehavior
and FileUploadHelper
classes) with the App:uses
function.
它需要从 Config/file_upload_settings.php 导入 FileUploadSettings
类和从 Vendor/upload.php 导入 Uploader
类.它可以通过 require_once
函数来完成,但我确信有一种 CakePHP 方法可以做到这一点.
It needs to import the FileUploadSettings
class from Config/file_upload_settings.php and the Uploader
class from Vendor/upload.php. It can be done with the require_once
function, but I'm sure there is a CakePHP way to do it.
推荐答案
根据 Cake 手册 App::import()
与 require_once()
的方式相当作品.据我了解,您将使用 App:uses()
加载类,使用 App:import()
加载供应商文件.
According to the Cake manual App::import()
is comparable to the way require_once()
works. From what I understand you would load classes using App:uses()
and Vendor files using App:import()
.
API 文档说以下 主题:
The API documentation says the following on the subject:
过去使用 App::import('Core', $class)
加载的所有类都需要使用 App::uses()
加载指的是正确的包.这一变化为框架带来了巨大的性能提升.
- 该方法不再递归查找类,它严格使用
App::build()
中定义的路径的值 - 将无法加载
App::import('Component', 'Component')
使用App::uses('Component', 'Controller');.
不再可能使用
App::import('Lib', 'CoreClass');
加载核心类.导入不存在的文件、提供错误的类型或包名称,或者$name
和$file
参数的null
值将导致错误的返回值.App::import('Core', 'CoreClass')
不再受支持,请改用App::uses()
并让类自动加载休息.加载供应商文件不会在供应商文件夹中递归显示,它也不会像过去那样将文件转换为下划线.
- The method no longer looks for classes recursively, it strictly uses the values for the paths defined in
App::build()
- It will not be able to load
App::import('Component', 'Component')
useApp::uses('Component', 'Controller');
. - Using
App::import('Lib', 'CoreClass');
to load core classes is no longer possible. Importing a non-existent file, supplying a wrong type or package name, ornull
values for$name
and$file
parameters will result in a false return value. App::import('Core', 'CoreClass')
is no longer supported, useApp::uses()
instead and let the class autoloading do the rest.- Loading Vendor files does not look recursively in the vendors folder, it will also not convert the file to underscored anymore as it did in the past.
迁移指南也有 关于
App:uses()
的一些事情要说,通常是一个很好的起点,可以比较 2.0 的最佳实践与 1.3 及更低版本的旧方法.
The migration guide also has some things to say about
App:uses()
and is a good starting point in general to compare best practices for 2.0 with the older methods from 1.3 and lower.
这个相关问题处理在 Cake 2.0 中加载供应商文件,我无法验证 José Lorenzo 的声明,即
App:import()
是 require_once()
的愚蠢包装器",也无法验证它是首选方式的声明包括文件.我能找到的关于后者的唯一参考是 编码标准 对于 Cake 贡献者,即.为 Cake 核心做出贡献的开发人员,而不是构建在该框架上的应用程序.
This related question deals with loading Vendor files in Cake 2.0, I can't verify the claim by José Lorenzo that
App:import()
is a "silly wrapper" for require_once()
, nor the statement that it's the preferred way of including files. The only reference I could find for the latter is in the Coding Standards for Cake contributors, viz. developers contributing to the Cake core, not applications built on the framework.
编辑
假设您要导入位于
Vendor/twitter 中的 Twitter OAuth 库
,主要的类文件是Vendor/twitter/twitteroauth/twitteroauth.php
中的twitteroauth.php
:
Let's say you want to import the Twitter OAuth library, residing in
Vendor/twitter
, the main class file is twitteroauth.php
in Vendor/twitter/twitteroauth/twitteroauth.php
:
App::import('Vendor', 'twitteroauth', array('file' => 'twitter'.DS.'twitteroauth'.DS.'twitteroauth.php'));
这篇关于在 CakePHP 2.1 插件中使用 App::uses(而不是 App::import)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!