php 在 CakePHP 2.0 中加载供应商文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8158129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading Vendor Files in CakePHP 2.0
提问by Johannes N.
I'm currently upgrading one of our projects to CakePHP 2.0. Unfortunately the "first line" of code makes problems, and I can't find a solution to that problem.
我目前正在将我们的一个项目升级到 CakePHP 2.0。不幸的是,代码的“第一行”产生了问题,我找不到解决该问题的方法。
In CakePHP 1.3 I had an App::import("Vendor", "facebook");
statement right before the AppController
class gets defined. The referenced file is located under /app/vendors/facebook/facebook.php
(and includes itself the base_facebook.php
file).
在 CakePHP 1.3 中,我在定义类App::import("Vendor", "facebook");
之前有一个声明AppController
。引用的文件位于/app/vendors/facebook/facebook.php
(并包括其自身base_facebook.php
)文件。
I tried many different ways to include the file now in CakePHP 2.0 according to the File naming and class loading described here: File naming and class loading changes in CakePHP 2.0
根据此处描述的文件命名和类加载,我尝试了许多不同的方法将文件现在包含在 CakePHP 2.0 中:CakePHP 2.0 中的文件命名和类加载更改
I renamed the path to app/Vendor/Facebook/Facebook.php
, or app/Vendor/Facebook/facebook.php
, and tried following methods:
我将路径重命名为app/Vendor/Facebook/Facebook.php
, or app/Vendor/Facebook/facebook.php
,并尝试了以下方法:
App::uses("Facebook", "Vendor/Facebook");
App::uses("Facebook", "Facebook");
App::uses("Facebook", "Vendor/Facebook/Facebook.php");
App::uses("Facebook", "Vendor");
Has anyone find a way to reference a vendor file yet? Because of the lazy loading the methods above do not fire an error/warning, so it's kind of annoying to debug this...
有没有人找到引用供应商文件的方法?由于延迟加载,上述方法不会触发错误/警告,因此调试它有点烦人......
回答by José Lorenzo Rodríguez
Vendors cannot be loaded using App::uses()
in CakePHP, this is because CakePHP cannot expect external libraries to follow the same standards regarding folder and file naming. You can still use App::import('Vendor', ...)
as you did in version 1.3 of the framework.
App::uses()
在 CakePHP 中无法加载供应商,这是因为 CakePHP 不能期望外部库遵循相同的文件夹和文件命名标准。您仍然可以App::import('Vendor', ...)
像在框架的 1.3 版中那样使用。
Now, using App::import()
for vendors is kind of silly, if you think about it. It is just an expensive, verbose and very silly wrapper for require_once()
.
现在,App::import()
如果您考虑一下,使用for vendor 有点愚蠢。它只是一个昂贵、冗长且非常愚蠢的require_once()
.
In 2.0, we actually encourage people to use require or require_once for their Vendor libraries. You can get the location of the Vendor folder using App::path('Vendor')
or just APP . 'Vendor' . DS
.
在 2.0 中,我们实际上鼓励人们为他们的供应商库使用 require 或 require_once。您可以使用App::path('Vendor')
或 仅获取供应商文件夹的位置APP . 'Vendor' . DS
。
回答by mmv_sat
Cake documentation suggest using App::uses() including-files-with-app-import
Cake 文档建议使用 App::uses() 包括-files-with-app-import
However, it also states if you have a non-stanard plugin to use App::Import()
但是,它还说明您是否有非标准插件来使用 App::Import()
App::import('Vendor', 'phpQuery', array('file' => 'bariew/phpquery/phpQuery/phpQuery.php'));
回答by Faisal
Assume you'r vendor file located /app/vendors/facebook/facebook.php
here.
假设您的供应商文件位于/app/vendors/facebook/facebook.php
此处。
The following line should do the same like App:: import() in the older version of CakePHP
下面这行应该像旧版本 CakePHP 中的 App::import() 一样
require_once(ROOT . DS . 'app' . DS .'Vendor' . DS . 'facebook' . DS . 'src' . DS . 'facebook.php');
$facebookApi = new facebook();