Question

im new to zend framework and would like to create a web portal which offers a services . The services would be used by webapplication and mobile application both. I'm using Chris Danielson's article

http://www.chrisdanielson.com/2009/09/02/creating-a-php-rest-api-using-the-zend-framework/) as the base for.

My question is am i going in the right direction .currently im accessing the services as http://www.zendrestexample.com/version .

1) I require it to use the url as http://www.zendrestexample.com/api/version

instead.

2)Do i need to use zend restserver for writing services ?

3)can i use the same service for both mobile app and the web app ?I mean any redirects problem arise?

4)Do i need to usezend rest client to consume those service ?

Pleas help me out..

Was it helpful?

Solution

Well you are not using bunch of PHP files to get this done... so I think you are on the right track =). The implementation in the article is okay, but very old... was written > 4 years ago. I would suggest looking into Zend_Soap_Server, Zend_Json_Server or Zend_Rest_Server. Soap solution is a bit heavy for the mobile in my opinion.

Just decide on the implementation and do little planning!


I wrote a web application and later had to add services layer in order to add mobile app interface to the application. Unfortunately this was not part of initial requirements so had to redo many things.

My advice is as follows (if you webapp and api are in a same project):

  1. code all your application logic in library or in controller helpers. So same code can be reused in the main web application and in API layer
  2. code your webapp logic in default module
  3. code your api layer in a dedicated module called 'api'
  4. phpdoc must be perfect in order for zend to autogenerate the SMD

For the API use standard JSON-RPC 2.0 protocol, there are clients for both Android / iPhone that utilize this and provide auto-discovery (SMD like WSDL but for json). All request sent via GET result in SMD being displayed all others result in handling of the request.

Utilize Zend_Json_Server for your API layer. Here is an functional example :

<?php

// API Controller Example
class ApiController extends Zend_Controller_Action
{

    public function init()
    {
        parent::init();
        $this->getHelper('ViewRenderer')->setNoRender();
    }

    public function helloWorldAction()
    {
        $this->_handleRequest('App_Api_HelloWorld');
    }

    protected function _handleRequest($handlerClassName)
    {
        //
        $this->getHelper('ViewRenderer')->setNoRender();

        //
        $server = new Zend_Json_Server();
        $server->setClass($handlerClassName);
        if ($_SERVER['REQUEST_METHOD'] == 'GET') {
            $cfg = Zend_Registry::get('config');
            $req = $this->getRequest();
            $reqUrl = $cfg->paths->basehref . $req->getControllerName() . '/' . $req->getActionName();

            $server->setTarget($reqUrl)
                    ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
            $smd = $server->getServiceMap();

            header('Content-Type: application/json');
            echo $smd;
        } else {

            //  handle request
            $server->handle();
        }
    }

}



// HANDLER Class Example

class App_Api_HelloWorld extends App_Api_ApiHandlerAbstract
{

    /**
     * says "hello world"
     * 
     * @return string
     */
    public function hello()
    {
        return 'hello world';
    }

    /**
     * says "hello $name"
     * 
     * @param string $name
     * @return string
     */
    public function hello2($name)
    {
        return "hello $name";
    }

    /**
     * 
     * @return string
     * @throws Exception
     */
    public function hello3()
    {

        throw new Zend_Json_Server_Exception('not allowed');
        return '';
    }

}

Here is sample Request (I added some bootstrap magic to pickup session by id)

https://domain.com/api/hello-world
{
  "session_id": "4ggskr4fhe3lagf76b5tgaiu57",
  "method": "hello2",
  "params": { 
    "name" : "Alex"
  },
  "id": 123
}

Review JSON RPC 2.0 Documentation.

I found Advanced REST Client for Google Chrome to be BEST extension for developing and testing JSON web services.

For additional security you can restrict ALL the request via HTTP Auth by adding few lines of code to the abstract controller or even create security controller plugin.

Good Luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top