問題描述
我目前正在將我們的一個項目升級到 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 中加載供應商文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!