CakePHP: Unable to load class from custom package
Asked Answered
N

2

3

We want to use the FPDF library in one of our controllers.

We created the following files:

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?

Neoteric answered 1/10, 2013 at 15:38 Comment(3)
Have you tried changing the name of the file fdpf_wrapper.php to FpdfWrapper.php and try with just App::uses('FpdfWrapper', 'Lib');? Also, I'm sure you've read this, but couldn't it be an error with the file (syntax, logic, etc) instead of cakephp not finding the class? If that doesn't work, can you be more specific and tell what fails everytime and how? Class not found error or something else?Heavensent
@Heavensent it was a class not found error. We're currently getting it to work with App::import('Vendor', bla bla);. Not sure whether Vendor or Lib is the most appropriate place for this.Neoteric
It just depends if it's a third party library or not (doc reference). Is it? and changing the name of the file to what I pointed out did not work?Heavensent
R
4

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.

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)));

And then use 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

Then you can use App::uses('FpdfWrapper', 'Lib/Fpdf');

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

Ruthannruthanne answered 1/10, 2013 at 17:30 Comment(0)
L
2

My case was a bit different.

To make App::uses('ExampleAPI', 'ExampleAPI') work make sure that:

  • /Lib/ExampleAPI/ExampleAPI.php exists and is readable
  • /Lib/ExampleAPI/ExampleAPI.php contains class ExampleAPI{} declaration
  • you call new ExampleAPI in the referring code
Lynelllynelle answered 14/5, 2016 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.