i am developing a Rest Controller using Zend Framework and i have to use an external php class in one of my action methods of the controller. How can i add this class or access it from my controller action method?
Do i need to write a custom library?
In my controller action method, iam fetching the polygon information from the database. Now i have an external php class that can validate whether a point is inside the polygon.
Googling on this issue, i found that we can write custom library classes for this sake. But iam unsure about this. So i want to know how can i include this external functionality in my controller action.
Please guide.
The simplest approach would be to just
includethe class, e.g.You can probably load this file with Zend_Loader or the Autoloader as well, but since the class name does’t follow the Pear/ZF naming convention, the easiest is this. It doesn’t matter where you put the actual class file as long as it is accessible somehow.
Since this is a third party class, you could extend it to make it match the ZF/Pear naming convention. Then place it into your library folder, so you can use the Autoloader:
This file would be placed in
APPLICATION_ROOT/lib/My/PointLoader.phpAnd in your bootstrap, register your "My_" namespace with
Then you can do
$pointLoader = new My_PointLoader;in your controller and the autoloader will take care of including the file.Or, when you want to add functionality or change the API of the class, aggregate it with an Adapter, e.g.
Then add the namespace prefix as shown above, so the Autoloader handles inclusion.