Loading Vendor Files in CakePHP 2.0
Asked Answered
K

3

35

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.

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

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

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...

Kaden answered 16/11, 2011 at 20:20 Comment(0)
C
54

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.

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().

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.

Cherise answered 16/11, 2011 at 20:33 Comment(8)
Hi Jose, thx for the quick reply. require_once works just fine - I hadn't thought of that. Nonetheless, the old App::import("Vendor", ...) syntax doesn't work for me.Kaden
Btw, the old App::import() usage as you show it will not work, because you need to provide the relative path to the class. import() will not look into the directories recursively anymore for performance reasons. I've heard reports about it, but no tickets opened in the issue tracker. The unit tests for that feature are all passing as expected, though. I will try to write a real-world example to actually validate that it works as expected. Thanks!Glenine
So App::import("Vendor", "Facebook/Facebook") should do the trick in your case.Tarrance
Why am I still getting "class 'Facebook' not found" though I have used require_once(APP . 'Vendor' . DS . 'facebook' . DS . 'facebook.php'); ? File is found but it can't somehow load it. I also tried giving more access rights to that folder.. @JoLatoyia
If that's actually the case @jose-lorenzo, don't you think it should be documented (book.cakephp.org/2.0/en/core-utility-libraries/…) that way? Also, what is the purpose / value of App::import or is it really just for legacy support?Maeda
App::import() is a method that was primarily used in cake 1.3, we had to leave it there for backwards compatibility reasons. There is no point in using it anymore. Maybe you want to help use document that in the book? :)Glenine
Vendors can be loaded with App::uses(). you misguide me. See here. This will not work only if when you have file name differ from class nameAlterant
@Alterant I think you are lost. App:uses() may break your code if the the library you are loading doesn't follow CakePHP standards. If it does, then App:uses() will work as you said but more often than not, you would load plugins written for Cake rather than loading them from Vendor.Buy
M
1

Cake documentation suggest using App::uses() including-files-with-app-import

However, it also states if you have a non-stanard plugin to use App::Import()

App::import('Vendor', 'phpQuery', array('file' => 'bariew/phpquery/phpQuery/phpQuery.php'));
Mcgough answered 25/8, 2016 at 21:36 Comment(0)
E
1

Assume you'r vendor file located /app/vendors/facebook/facebook.php here.

The following line should do the same like App:: import() in the older version of CakePHP

 require_once(ROOT . DS . 'app' . DS .'Vendor' . DS  . 'facebook' . DS . 'src' . DS . 'facebook.php');

 $facebookApi = new facebook();
Enjoin answered 26/1, 2017 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.