我正在Cake
PHP 2.1中编写一个小应用程序,我想使用Nick Baker的文件上传插件.我下载了cakePHP2.0分支(我知道还没有完成),并将其放在我的应用程序Plugin文件夹中.我做了一些必要的修改,但我好奇的是,正确的方法是用App:uses函数替换App :: import函数调用(在FileUploadComponent,FileUploadBehavior和FileUploadHelper类的起点).
它需要从Config / file_upload_settings.PHP导入FileUploadSettings类,并从Vendor / upload.PHP导入Uploader类.可以使用require_once函数完成,但我确定有一个CakePHP方法可以实现.
根据Cake手册,App :: import()与require_once()的工作方式相当.根据我的理解,您将使用App:uses()和Vendor文件使用App:import()加载课程.
原文链接:https://www.f2er.com/php/131685.htmlAPI文档说the following主题:
All classes that were loaded in the past using
App::import(‘Core’,$class)
will need to be loaded usingApp::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 inApp::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.
迁移指南还有some things to say关于App:uses(),并且是将2.0的最佳实践与1.3及更低版本的较旧方法进行比较的良好起点.
This related question处理在Cake 2.0中加载供应商文件,我无法验证JoséLorenzo的声明:App:import()是require_once()的“傻包装”,也不是包含文件的首选方式的声明.我可以找到的唯一的参考是在Coding Standards的蛋糕贡献者,即开发人员为Cake的核心做贡献,而不是构建在框架上的应用程序.
编辑
假设您要导入Twitter OAuth library,驻留在Vendor / twitter中,主要类文件是Vendor / twitter / twitteroauth / twitteroauth.PHP中的twitteroauth.PHP:
App::import('Vendor','twitteroauth',array('file' => 'twitter'.DS.'twitteroauth'.DS.'twitteroauth.PHP'));