Zend Framework getAction method I am new to MVC and Zend, so I have little idea what I am doing at the moment, but here is my question.

The following code is my get method for an API I am attempting to build in order to learn both. It works as is, but given the sparsity of code I have seen in example methods, and the fact that Zend seems to do a lot for you, is there a better way of doing this? The code is something I cobbled together because the tutorial that I was walking through forgot to add the get method code.

public function getAction()
{
    // action body
    // Is there a better way to get the id from the URL?
    $this-&gt;_getAllParams();
    $request = $this-&gt;_request-&gt;getParams();

    if (array_key_exists($request['id'], $this-&gt;_todo)){
        $return[] = array($request['id'] =&gt; $this-&gt;_todo[$request['id']]);
    }else{
        // error code here or call some error method?
        // do I do error coding here or send a message to some handler/helper?
    }
    echo Zend_Json::encode($return);
}


Essentially this will take a URL and pass json encode information that is currently stored in an array, i.e:

http://restapi.local/api/1


Will display:

[{"1":"Buy milk"}]


The routing takes place in the bootstrap:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    public function _initRoutes()
    {
        $front = Zend_Controller_Front::getInstance();
        $router = $front-&gt;getRouter();
        $restRoute = new Zend_Rest_Route($front);
        $router-&gt;addRoute('default', $restRoute);
    }

}


Below is the full ApiController.php:

class ApiController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
        $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(true);
        $this-&gt;_todo = array (
                    "1" =&gt; "Buy milk",
                    "2" =&gt; "Pour glass of milk",
                     "3" =&gt; "Eat cookies"
        );
    }

    public function indexAction()
    {
        // action body
        echo Zend_Json::encode($this-&gt;_todo);
    }

    public function getAction()
    {
        // action body
        $this-&gt;_getAllParams();
        $request = $this-&gt;_request-&gt;getParams();
        if (array_key_exists($request['id'], $this-&gt;_todo)){
            $return[] = array($request['id'] =&gt; $this-&gt;_todo[$request['id']]);
        }
        echo Zend_Json::encode($return);
    }

    public function postAction()
    {
        // action body
        $item = $this-&gt;_request-&gt;getPost('item');

        $this-&gt;_todo[count($this-&gt;_todo) + 1] = $item;

        echo Zend_Json::encode($this-&gt;_todo);
    }

    public function putAction()
    {
        // action body
    }

    public function deleteAction()
    {
        // action body
    }
}
By using Zend_Rest_Route you are on a good way. I wouldn't recommend making it the default route though, as I had trouble when using MVC-stuff which required additional params not specified in the route, e.g. for a paginator. You could also move the routing-configuration to your application.ini to keep your Bootstrap-class clean.

I recommend simplifying your getAction() by using:

$id = $this-&gt;getRequest()-&gt;getParam('id', null);
$id = $this-&gt;_request-&gt;getParam('id');


Both are identical, but I prefer using the get-methods in case I use them to override the handling of the underlying class-property (in this case $this-&gt;_request). The second argument specifies the default value to be used, if the param is not set (by default this is null).

edit: Also, instead of

$this-&gt;_todo[count($this-&gt;_todo) + 1] = $item;

$this-&gt;_todo[] = $item;


this will automatically append $item to $this->_todo, even if the variable is not yet initialized.