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
 }
}
Ethan Bierlein
15.9k4 gold badges59 silver badges146 bronze badges
asked Jun 23, 2012 at 4:47
\$\endgroup\$
1
  • \$\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\$ Commented Jun 23, 2012 at 8:15

1 Answer 1

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.

answered Jun 23, 2012 at 20:52
\$\endgroup\$
1
  • \$\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\$ Commented Jun 23, 2012 at 22:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.