问题描述
我们想在我们的控制器中使用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?
推荐答案
首先,包路径必须注册才能与, 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' );
或更好地添加新软件包:
or better add a new package:
App::build(array('Lib/Fpdf' => array(APP . 'Lib' . DS . 'Fpdf' . DS)), App::REGISTER);
然后你可以使用 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:无法从自定义包中加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!