Loading vendor files
The traditional way to load files from the “vendors” folders — using the vendor() function — has been deprecated a while ago in the development branch (you get a warning if you use this function).
The new way is to use App::import(). To load a file named “example.php”, you have to use the following snippet:
App::import('Vendor', 'example');
Unfortunately, there’s a snag to it: it only works if the name is in lowercase, i.e. the following snippet won’t load the file “Example.php”:
App::import('Vendor', 'Example');
Instead you have to use:
App::import('Vendor', 'example', array('file' => 'Example.php'));
The second parameter can be any string except an empty string or null, but I don’t know what the meaning of this parameter is in this context…
The same also applies for folder names:
App::import('Vendor', 'example'.DS.'example'); // loads example/example.php
App::import('Vendor', 'example', array('file' => 'Example'.DS.'example.php')); // loads Example/example.php
It’s quite illogical, but according to some newly added tests it is supposed to work in this way…




“CakePHP: the rapid development php framework.”
lol
“CakePHP: the rapid development php framework.”
App::import(’Vendor’, ‘example’, array(’file’ => ‘Example’.DS.’example.php’)); // loads Example/example.php
lol
nao, you apparently haven’t seen how the Zend Framework imports files into its bootstrap. It’s about the same as Cake’s long example above (last I’ve seen it anyhow, they may have updated it). Cake truly does make development much more rapid, regardless of some nuances that Dan will tell us about (so that we do not run into the same problems that he does).
@nao, Brendon: Thanks for your comments!
@nao: *g*
@Brendon: I think the problem is not necessarily that you have to write more, the problem is that the current solution is inconsistent. And that’s what slows you down, not the typing of some more characters.
Daniel, thanks for the info. Hope the development team correct this. I don’t experience problems with this but I really wont like to see app crashing while I upgrade the framework.
to be fair, I think you should include the offical trac mentions of this - it seems to point to a naming convention (dot syntax?) being implemented in this re-mixed vendor import code.
https://trac.cakephp.org/changeset/6600
@Nik: Yes, I also hope it will get corrected, but I think it won’t happen as recently two respective tickets were closed and tests were added to verify this behavior…
@luke: Yes, it is mentioned that the failing approaches don’t follow the naming conventions for file and directory names. But according to the manual there are no conventions for vendors ;-)
ah, didnt know that… but doesnt that just mean it is a problem with the manual therefore ;)
What happend to the good old vendor inclusion function ?
vendor(’Example’);
For cake 1.2 couldn’t it be made like this :
App::import(’Vendor’, array(’file’ => ‘Example.php’));
this definitely makes vendor imports far more difficult to use than they used be. But looking at how App::import works. There isn’t a lot of room for adapting something simpler without having to rewrite the entire app:import. Another option is to always specify the 3rd parameter. That would at least keep your vendor imports looking clean and predictable.
@luke: The explanation in the manual makes sense to me :)
@blueballoon: Personally I would prefer the same syntax as you could use with the vendor function:
App::import('Vendor', array('Example1', 'Example2'));@Mark: Yes, specifying always the 3rd parameter is a possible option. Another option is to copy the code of the vendor() function and to create your own “vendor” function in bootstrap.php.
But those are only workarounds, the main problem remains. I think it wouldn’t be that difficult to refactor App::import() accordingly. In the simplest case you could check whether the type is “Vendor” and then call a method which handles the import of vendor files.