Reputation: 7094
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.
Upvotes: 3
Views: 7179
Reputation: 317009
The simplest approach would be to just include
the class, e.g.
class PolygonController …
public function someAction()
{
include 'path/to/class/file.php';
$pointLocation = new pointLocation;
// do something $pointLocation;
}
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:
<?php
include 'path/to/class/file.php';
class My_PointLoader extends pointLoader {}
This file would be placed in APPLICATION_ROOT/lib/My/PointLoader.php
And in your bootstrap, register your "My_" namespace with
$autoloader->registerNamespace('My_');
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.
<?php
include 'path/to/class/file.php';
class My_PointLoader_Adapter
{
protected $_pointLoader;
public function __construct(pointLoader $pointLoader = NULL)
{
if($pointLoader === NULL) {
$pointLoader = new pointLoader;
}
$this->_pointLoader = new pointLoader;
}
public function someAdapterMethod($foo)
{
$this->_pointLoader->someMethodInPointLoader($foo);
}
// … more methods
}
Then add the namespace prefix as shown above, so the Autoloader handles inclusion.
Upvotes: 4
Reputation: 57268
My approach would be something like:
Zend_Maths_Point_Location
Zend/Maths/Point
Location.php
and place within the above folderIn Your controller use:
$Point = new Zend_Maths_Point_Location
This basically just integrates the class into the Zend Autoloader spec, and makes it more fluent.
You will need to follow the Zend docs carefully to make sure it slots in without any issues
Upvotes: 5