問題描述
我們想在我們的一個控制器中使用 FPDF 庫.
We want to use the FPDF library in one of our controllers.
我們創建了以下文件:
app
-Lib
--Fpdf
---files.php
---fpdf.php
---fdpf_wrapper.php <-- this is our class (FdpfWrapper) which extends the base FPDF class
就在控制器類之前,我們試試這個:
Right before the controller class, we try this:
App::uses('FpdfWrapper', 'Lib/Fpdf');
但每次都失敗.我們做錯了什么?
But it fails every time. What are we doing wrong?
推薦答案
首先,必須注冊包路徑才能與 App::uses
和 Lib/Fpdf
沒有這樣的,默認只注冊核心包.
First of all, package paths must be registered in order to be used with App::uses
, and Lib/Fpdf
is no such one, by default only the core packages are registered.
您可以擴展現有包的路徑,在您的情況下為 Lib
:
You could either extend the paths for an already existing package, in your case that would be Lib
:
App::build(array('Lib' => array(APP . 'Lib' . DS . 'Fpdf' . DS)));
然后使用 App::uses('FpdfWrapper', 'Lib');
http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#adding-paths-for-app-to-find-packages-in
或者更好地添加一個新包:
or better add a new package:
App::build(array('Lib/Fpdf' => array(APP . 'Lib' . DS . 'Fpdf' . DS)), App::REGISTER);
http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#add-new-packages-to-an-application
然后你可以使用 App::uses('FpdfWrapper', 'Lib/Fpdf');
最后但并非最不重要的一點,當然文件名必須遵循@Nunser 已經提到的 CakePHP 約定,即 fdpf_wrapper.php
必須重命名為 FdpfWrapper.php
And last but not least, of course the filename must follow the CakePHP conventions as already mentioned by @Nunser, ie fdpf_wrapper.php
must be renamed to FdpfWrapper.php
這篇關于CakePHP:無法從自定義包加載類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!