3
\$\begingroup\$

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->_getAllParams();
    $request = $this->_request->getParams();

    if (array_key_exists($request['id'], $this->_todo)){
        $return[] = array($request['id'] => $this->_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->getRouter();
        $restRoute = new Zend_Rest_Route($front);
        $router->addRoute('default', $restRoute);
    }

}

Below is the full ApiController.php:

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

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

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

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

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

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

    public function putAction()
    {
        // action body
    }

    public function deleteAction()
    {
        // action body
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can't comment on the Zend stuff since I don't quite understand what's going on here, but the first thing that jumps out at me is that it's possible for your encode call to act on a non-existing variable. (Also, I suspect that it should be returned instead of echo'd.) Can you post the entire class? I'm a bit confused about what _todo is. \$\endgroup\$ – Corbin Jun 23 '12 at 8:15
2
\$\begingroup\$

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->getRequest()->getParam('id', null);
$id = $this->_request->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->_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->_todo[count($this->_todo) + 1] = $item;

$this->_todo[] = $item;

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

\$\endgroup\$
  • \$\begingroup\$ Thanks, I should have looked deeper into the $this->getRequest()->getParam(). That is a much simpler way, and makes checking for errors simpler as well. \$\endgroup\$ – SuperMykEl Jun 23 '12 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.