Help! vendor() is deprecated.

No, I don't need help - I am the one providing it :)

Use of vendor() function to load third party libs in CakePHP has been deprecated for some time now... It has been replaced with the more generic App::import() the usage is simple

PHP:
  1. App::import('vendor', 'filename');

And it works fine.... now now! I wouldn't write a blog entry for this trivial a thing even though I do need the traffic. Hey! anyone who blogs and says she does not need the traffic is a liar, but I digress here.

You soon run into trouble with the above syntax when you have a file which has a underscore in its name eg: if you have file_name.php then

PHP:
  1. App::import('vendor', 'file_name');

does not work. You have to write

PHP:
  1. App::import('vendor', 'fileName');

Still no big deal but the above code stops dead in its tracks if you have a vendor file name File_Name.php or worse if the vendor files are to be inside sub directories

Damn it! why can't we enforce all the other developers to name their files properly? After all CakePHP is...
because my dear, we live in a real world and thankfully in real world there are solutions. The syntax to use is

PHP:
  1. App::import('vendor', 'uniqueidentifier', array('file'=>'path/relative/to/vendor/File_Name.php);

In my case I ended up with

PHP:
  1. App::import('Vendor', 'SparklineBar', array('file'=> 'sparkline/lib/Sparkline_Bar.php'));

It may be noted that App::import() returns true on success which you can use to check successful loading of our vendor file.

Well, it is an entirely different matter than Sparkline class has a base class called very imaginatively "Object" and I was no better off in the end....

P.S. Thanks everyone for dropping by and increasing traffic and yes I did add this to http://book.cakephp.org/ but you would have never known if I had not said so.

About Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

Leave a Reply