3

I am building an application using zend framework.

The question is, how to use the same controller logic of the Zend_Rest_Controller within a non-REST app.

For instance, let's assume twitter was written with Zend Framework. They would probably use the Zend_Rest_controller and Route for their API. However, what would they use for their website (which obviously use the same API logic)? Would they write an entire new application that simply fire REST request? Isn't that overload.

[EDIT]
If web app calls API through some http_client class to get the data, that makes another request to server (it causes performance degrade and slow down the response). I don't want to make another request and want to use the same business logic which is in API.

Thanks,
Venu

Venu
  • 7,243
  • 4
  • 39
  • 54
  • @ Venu Gopal T dear accept the answer given on your questions by others. By clicking the Tick sign with each answer – Awais Qarni Apr 29 '11 at 12:08

2 Answers2

2

New Answer:

I have come up with a pattern that seems to work well. It solves all of your concerns: Here is a scaled down version of what I came up with:

First we need our own controller. This conroller will have a service where by it proxies any action request to the service, if they are not defined:

abstract class App_Rest_Controller extends Zend_Controller_Action
{
    /** 
     * @var App_Rest_Service_Abstract
     */
    protected $_service;

    public function __call($methodName, $args)
    {
        if ('Action' == substr($methodName, -6)) {
            $action = substr($methodName, 0, strlen($methodName) - 6);
            return $this->_service()->$action();
        }

        return parent::__call($methodName, $args);
    }
}

Now its time for the service. We extend Action Helper Abstract so that:

  1. we have direct access to the request object
  2. we can easily call up the service from any controller

This will act a go between for the the application and the actual storage of the data.

abstract class App_Rest_Service_Abstract extends Zend_Controller_Action_Helper_Abstract
{
    /*
     * @var App_Rest_Storage_Interface
     */
    protected $_storage;
    public function __call($methodName, $args)
    {
        if (!method_exists($this->getStorage(), $methodName)) {
            throw new App_Rest_Service_Exception(sprintf('The storage does not have the method "%s"', $methodName));
        }

        switch ($methodName) {
            case 'get':
            case 'put':
            case 'delete':
                //if id param isnot set, throw an exception
                if (FALSE === ($id = $this->getRequest()->getParam('id', FALSE))) {
                    throw new  App_Rest_Service_Exception(sprintf('Method "%s" expects an id param, none provided', $methodName));
                }
                $iterator = $this->getStorage()->$methodName($id, $this->getRequest()->getParams());
                break;
            case 'index':
            case 'post':
            default:
                //if index, post or not a tradition RESTful request, the function must expect the first and only argument to be an array
                $iterator =  $this->getStorage()->$methodName($this->getRequest()->getParams());
                break;
        }


        return $this->_getResult($iterator);
    }

    protected function _getResult($iterator)
{ /*
       * write your own, in my case i make a paginator and then
       *  either return it or send data via the json helper
       * 
      /*
}

Now for the interface. This will do the actual work of storing, modifying and returning data. The beauty of using it as an interface is that you can easily implement it no matter what you use for the model layer. I created an abstract Storage that simply has a Zend_Form (for validation) and a Zend_Db_Table for the actual data. but you could also implement it on any object.

interface App_Rest_Storage_Interface extends Zend_Validate_Interface
{
    public function index(array $params = NULL);

    public function get($id, array $params = NULL);

    public function post(array $params);

    public function put($id, array $params);

    public function delete($id, array $params);

}

Now operation anywhere within your site. Suppose you have an "Customers" service. Inside any controller its as simple as

$customer = $this->_helper->helper->customers->get(1);

anywhere else (say a view helper for example):

Zend_Controller_Action_HelperBroker::getStaticHelper('customers')->get(1)

I hope this helps. It is working well for me.

Fatmuemoo
  • 2,187
  • 3
  • 17
  • 34
  • Yes I agree with your solution. But one thing to consider 1) Why we need to send the JSON data from the helper, instead we can send the php object and let rest controller decide in which format it should deliver. So it decreases the encoding and decoding while using it in web controllers. – Venu May 07 '11 at 06:13
0

Disclaimer: I've never done this, I don't know how feasible it is.

Since Zend_Rest_Controller extends Zend_Controller_Action with now real rest specific logic except for a few abstract methods, you could just have your website controllers extend the Rest controller. Example:

class Web_IndexController extends Rest_IndexController
{
    public function IndexAction() {
        //do whatever your rest contrller would do
        $result = parent::indexAction();
        //add website specific specific logic here
    }
}

If your rest controller's actions are returning values, such as db objects or arrays based upon what happened, you could then use the returned data to do web site-specific logic.

One thing to think about, if you are using the json action helper to return values for your rest api, you should build in a controller property to suppress the sending and exiting. Example:

class Rest_IndexController extends Zend_Rest_Controller
{

    protected $_sendJson = TRUE;
    public function IndexAction() {
        $this->_helper->json($data, $this->_sendJson);
    }
}

class Web_IndexController extends Rest_IndexController
{
    protected $_sendJson = FALSE;
}

Happy Hacking!

Fatmuemoo
  • 2,187
  • 3
  • 17
  • 34
  • Hey thanks to your reply!! I was thinking of having common business logic layer for api and web (some php class which connect to db table for data), but your idea seems to be much better. I tried to follow your suggestion, but couldn't able to extend a class which is in another module. – Venu Apr 29 '11 at 13:54
  • I was using auto loading, which didn't load the class in another module. Later I included the class which needs to extend. It worked!! – Venu Apr 29 '11 at 14:26
  • Few issues still there with this design.. 1) Say web_indexcontroller needs to call two methods of different api classes? 2) how to auto load all the classes in another module? – Venu Apr 29 '11 at 14:54
  • You are going to have to add a service layer to your app. Move all business logic to the service layer. The controllers would only handle data returned by the service layer. See http://stackoverflow.com/questions/2458195/how-to-implement-service-layer-in-zend-framework on how to implement a service layer in ZF. – Fatmuemoo Apr 29 '11 at 15:25
  • I have few questions on service layer.. 1) Where does the validation goes? is it in service layer or in controller? 2) If i user service layer, how can I user helper broker? I have add helper broker object to service layer class? 3) Will service layer directly work $_POST or $_GET params? or it will receive params as arguments? My aim is to develop API for the app first and use the api for different front ends.. – Venu May 03 '11 at 09:58
  • Venu, A need for something very similar has come up in a web app i'm developing. I think the solution I'm going to use is to move all the business logic into the service layer and have the classes in that service layer extend Zend_Controller_Action_Helper_Abstract in order to easily plug-in to Zend's existing helper broker architecture and have complete access to the request object. In the next few days, I'll post my solution. – Fatmuemoo May 04 '11 at 01:29
  • Yeah I was thinking on that. But for time being, I have created objects of service layer through reflection class and using them (Though request object is not available). Thanks for your support. – Venu May 04 '11 at 13:20