3

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.

0

2 Answers 2

5

My approach would be something like:

  • Rename the class to Zend_Maths_Point_Location
  • Create the following sub folder in zend Zend/Maths/Point
  • Name the file Location.php and place within the above folder
  • In 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

Sign up to request clarification or add additional context in comments.

2 Comments

Since this is not a ZF component, you should not use the Zend prefix or folder for that. Prefix it with something else and then just register the prefix with the Autoloader. That prevents accidental deletion when you update the ZF library folder. It might also confuse other developers that stumble across the class and wonder why they cannot find any docs for that in the official ZF or why it is using PHP4 syntax.
Finally this is what i did for autoloader approach. I created a folder My/Polygon inside ZendFramework/library, and created a file called: "Point.php" there. Now i renamed the class to: My_Polygon_Point and inside my controller, i accessed it like: $pointLocation = new My_Polygon_Point();
4

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.

5 Comments

But where do i need to place this class file? Inside my controllers or models or views ?
@hsz please point me to the ZF docs where it says: you have to use Zend_Loader. There is absolutely no reason to use all of the framework components, just because they exist.
Both approaches, including the file, and using the library (as pointed by RobertPitt) work well. I did not try the adapter approach though. Thanks for the answers.
One should make use of the include_once and require_once functions, instead of only include or require. The include or require functions are for where you intend to use the code multiple times, in multiple places. include_once and require_once allow you to include the files only if they've not been included before. This prevents trying to re-define the same classes multiple times.
@FrozenFire not necessarily as *_once is potentially slower due to it having to check whether the file was already included or not. If you know this is the only call to that particular file, there is no need to include_once. µ-optimization though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.