A few days back I thought of implementing a RESTful web service. I did not have any idea on how to implement one. So I searched online and found this post. Based on astrumfutura’s post, I created my own “simplified” version of it.
Before anything else, I want to point out that I access the XML generated by Zend_Rest_Server using the following URL:
http://localhost/api/emp/id/123456
Soon, I will be able to make something like this:
http://localhost/api/emp/manager/123456
And get the manager of the employee using the ID.
Then I would get an XML page that would look like this:
− <emprestserver generator="zend" version="1.0"> − <getbyid> <emp_id>123456</emp_id> <emp_lname>Del Rosario</emp_lname> <emp_fname>Wenbert</emp_fname> <emp_mname /> <emp_status>A</emp_status> <emp_type>R</emp_type> <emp_gender>M</emp_gender> <status>success</status> </getbyid> </emprestserver> |
Assuming that you already have a working ZF application on hand, create a new controller named “api”.
< ?php /** * /application/controllers/ApiController.php * * ApiController * * This will contain all the API stuff you want for your application * */ class ApiController extends Zend_Controller_Action { /** * @var Zend_Rest_Server */ protected $_server; public function init() { $this->_helper->viewRenderer->setNoRender(); //disable Layouts for this controller $this->_helper->layout()->disableLayout(); } public function indexAction() { $this->_redirect('/'); //for anything other than a call to service/params redirect to indexControlller :P } public function empAction() { $params = $this->_getAllParams(); unset($params['controller']); unset($params['action']); unset($params['module']); $param_keys = array_keys($params); $filter_params = $params; //need to filter this $param_keys_uc = array(); foreach($param_keys AS $key) { $param_keys_uc[] = ucfirst($key); } $methodname = "getBy".implode('',$param_keys_uc); $request = array('method' => $methodname); foreach($param_keys AS $key) { $request[$key] = $filter_params[$key]; //need to filter key //need better checking if(!$request[$key]) { throw new Exception($request[$key].' contained invalid data.'); } } require_once 'EmprestServer.php'; $server = new Zend_Rest_Server(); $server->setClass('EmprestServer'); $server->handle($request); } } |
Then the EmprestServer Class would look something like this:
< ?php /** * /application/controllers/EmprestServer.php * * Employee Rest Server * * I decided to put this beside the other controllers because I thought * this would "act" like a controller. * * I plan to add more methods after this and I want it to be a part of * my entire application. * */ //I am using the Employee Model for easier access to the database require_once APPLICATION_PATH.'/models/Employee.php'; class EmprestServer { /** * /api/emp/id/1124921 * * @param unknown_type $emp_id * @return unknown */ public function getById($id) { return $this->getData(array('id'=>$id)); } public function getByShortname($shortname) { return $this->getData(array('shortname'=>$shortname))->toArray(); } private function getData(array $params) { $emp = new Employee(); $data = array(); foreach($params AS $key=>$value) { if($key=='id') { //getEmployeeById($value) is a method in my Employee Model if(!$data = $emp->getEmployeeById($value)) throw new Exception('Employee ID not found.'); } $data = $data->toArray(); } return $data; } } |
Note that the code still has a long way to go (filtering, etc). But with that, I was able to have a RESTful server up and running — with one method at the moment.
If you are looking for more information using Zend Framework, I want to reiterate that I based this post on this post.
Comments / suggestions are welcomed and encouraged!
Pingback: Zend Framework: Handling custom XML reponses using Zend_Rest_Server | eKini: Web Developer Blog