在CakePHP 2.1插件中使用App :: uses(而不是App :: import) [英] Using App::uses (instead of App::import) in a CakePHP 2.1 Plugin
问题描述
我是CakePHP的新手,但我开始在CakePHP 2.1中编写一个小应用程序,我想使用Nick Bakers文件上传插件。我下载了cakephp2.0分支(我知道还没有完成),放在我的应用程序Plugin文件夹,并对其进行了一些必要的修改,但我很好奇什么是正确的方式取代这些App :: import函数调用(在FileUploadComponent,FileUploadBehavior和FileUploadHelper类的开始)与App:uses函数。它需要从Config / file_upload_settings.php和Uploader类从Vendor / upload.php导入FileUploadSettings类。它可以用require_once函数来完成,但我相信它有一个CakePHP的方式来做。
感谢通知
根据Cake手册 App :: import / code>相当于
require_once()
的工作方式。从我的理解,你将使用 App:uses()
和供应商文件使用 App:import()
API文档说关于此主题的以下:
过去使用
App :: import('Core',$ class)
将需要使用App :: uses()
包。
- 该方法不再递归地查找类,它严格使用
App :: build()
- 中定义的路径的值将无法加载
:: import('Component','Component')
使用App :: uses('Component','Controller');
li>
- 使用
App :: import('Lib','CoreClass');
加载核心类不再可能。导入不存在的文件,提供错误的类型或程序包名称或$ name
和null
c $ c> $ file 参数将导致错误的返回值。
App :: import('Core','CoreClass ')
不再支持,请改用App :: uses()
,让类自动加载完成其余操作。
- 加载供应商文件不会在供应商文件夹中递归地查找,它也不会像以前一样将文件转换为下划线。
迁移指南还包含一些要说的应用:uses()
,并且是一个很好的起点,
这个相关的问题处理在Cake 2.0中加载供应商文件,我不能验证JoséLorenzo的声明, App:import()
是 require_once()
的蠢包装,也不是语句包含文件的首选方式。我可以找到的唯一参考是在蛋糕的编码标准贡献者
$ b
p>假设您要导入位于
供应商/ twitter $ c中的 Twitter OAuth库 $ c>,主类文件 twitteroauth.php
在供应商/ twitter / twitteroauth / twitteroauth.php
: App :: import('Vendor','twitteroauth',array('file'=>'twitter'.DS。 'twitteroauth'.DS.'twitteroauth.php'));
I'm new to CakePHP , but I started to write a small application in CakePHP 2.1 and, I want to use Nick Bakers File Upload Plugin. I downloaded the cakephp2.0 branch (I know that isn't done yet), placed in my apps Plugin folder and made some necessary modifications on it, but I'm curious what is the right way replacing those App::import function calls (at start of FileUploadComponent, FileUploadBehavior and FileUploadHelper classes) with the App:uses function. It needs to import the FileUploadSettings class from Config/file_upload_settings.php and Uploader class from Vendor/upload.php. It can be done with the require_once function, but I'm sure it has a CakePHP way to do it.
Thanks in advice
解决方案 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()
.
The API documentation says the following on the subject:
All classes that were loaded in the past using App::import(‘Core’, $class)
will need to be loaded using App::uses()
referring to the correct package. This change has provided large performance gains to the framework.
- 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')
use App::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, or null
values for $name
and $file
parameters will result in a false return value.
App::import('Core', 'CoreClass')
is no longer supported, use App::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.
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.
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.
EDIT
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!