As in my previous post about creating an administrator panel in CakePHP, my another programmer needed my help in telling him more about vendors.
This was his questions:
What are vendors and why are they used ?
Vendors are used in CakePHP to use external PHP classes/libraries. Lets say you downloaded a class from some great classed database like phpclasses.org or you had your own class already developed, and want to use it in you Cake application. You can use vendor for it. Its just like include() in PHP, however its a bit different. The vendors folder is where you’ll place third-party PHP libraries you need to use with your CakePHP applications.
For example:
Lets say we have a file my_library.php, we'll put it in /app/vendors.
my_library.php is as follows:
-
class Area
-
{
-
function calculate_area($length, $width)
-
{
-
return $length*$width;
-
}
-
}
In order to use this class inside my Cake app, I'll have to tell Cake about that. To use this in a controller, I can write following code in my controller, lets say my controller is items_controller.php, and it contains a function called show_area():
-
function show_area($length, $width)
-
{
-
vendor('my_library'); //this will just include my_library.php at this stage
-
$area=&new Area();
-
$current_area=$area->calculate_area($length, $width);
-
$this->set('area', $current_area);
-
}
Using this way, we can use 3rd party classes inside our controllers / models / view and also in our components / behaviors / helpers.
Many people ask this question:
Why are there two “vendors” folders in CakePHP?
- Abhimanyu Grover