沃梦达 / 编程问答 / php问题 / 正文

在 CakePHP 2.0 中加载供应商文件

Loading Vendor Files in CakePHP 2.0(在 CakePHP 2.0 中加载供应商文件)

本文介绍了在 CakePHP 2.0 中加载供应商文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我们的一个项目升级到 CakePHP 2.0.不幸的是,代码的第一行"出现了问题,我找不到解决该问题的方法.

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 1.3 中,我在定义 AppController 类之前有一个 App::import("Vendor", "facebook"); 语句.引用的文件位于 /app/vendors/facebook/facebook.php 下(并包括 base_facebook.php 文件).

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 2.0 中:CakePHP 2.0 文件命名和类加载变化

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

我将路径重命名为app/Vendor/Facebook/Facebook.php,或app/Vendor/Facebook/facebook.php,并尝试了以下方法:>

I renamed the path to app/Vendor/Facebook/Facebook.php, or app/Vendor/Facebook/facebook.php, and tried following methods:

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...

推荐答案

在 CakePHP 中无法使用 App::uses() 加载供应商,这是因为 CakePHP 不能期望外部库遵循相同有关文件夹和文件命名的标准.您仍然可以像在框架的 1.3 版中那样使用 App::import('Vendor', ...).

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::import() 有点愚蠢.它只是 require_once() 的一个昂贵、冗长且非常愚蠢的包装器.

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().

在 2.0 中,我们实际上鼓励人们为他们的供应商库使用 require 或 require_once.您可以使用 App::path('Vendor') 或仅使用 APP 来获取 Vendor 文件夹的位置.'小贩' .DS.

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.

这篇关于在 CakePHP 2.0 中加载供应商文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 CakePHP 2.0 中加载供应商文件