我们想在我们的一个控制器中使用 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模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽