15. Zend Framework You need one more thing to enable modules: resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" resources.modules[] = Plus, we want our own libraries, eventually: autoloaderNamespaces.kingfoo = "Kingfoo_"
21. Web services From Wikipedia: Web services are typically application programming interfaces (API) or web APIs that are accessed via Hypertext Transfer Protocol (HTTP) and executed on a remote system hosting the requested services. Web services tend to fall into one of two camps: Big Web Services and RESTful Web Services.
55. XML-RPC with ZF SERVER $server = new Zend_XmlRpc_Server(); $server->setClass('Kingfoo_Quote','zfwsdemo'); echo $server->handle(); CLIENT $client = new Zend_XmlRpc_Client('http://presentation-zfws/XMLRPC.php'); //get list of methods $result = $client->call('system.listMethods'); //get a random quote $result = $client->call('zfwsdemo.randomQuote');
62. JSON-RPC with ZF SERVER $server = new Zend_Json_Server(); $server->setClass('Kingfoo_Quote'); $server->handle(); CLIENT $request = new stdClass(); $request->jsonrpc = '2.0'; $request->method = 'getQuote'; $request->params = array(1); $request->id = time(); $json = Zend_Json::encode($request); $client = new Zend_Http_Client('http://presentation-zfws/JSONRPC.php'); $response = $client->setRawData($json, 'application/json')->request('POST');
63. JSON-RPC with ZF AND NOW WITH SMD $server = new Zend_Json_Server(); $server->setClass('Kingfoo_Quote'); if ('GET' == $_SERVER['REQUEST_METHOD']) { $server->setTarget('/JSONRPC.php') ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2); $smd = $server->getServiceMap(); header('Content-Type: application/json'); echo $smd; return; } $server->handle();
71. RESTafarians leave funny comments on blogs and have strong opinions. True SOAP believers are a dying race, it appears. Have a laugh, Google for 'The S stands for Simple'
72. REST GET POST PUT DELETE /quotes Get list Add quote to list Update entire list Delete entire list /quotes/<id> Get quote with id <id> Add subquote (unlikely) Update single quote Delete single quote List = oftern referred to as a 'collection'
73.
74. Now more and more integrated, using Zend_Rest_Controller and Zend_Rest_Route
75. Future versions of ZF (2.0?) will probably have it fully integrated in the controller, using context switching (see posts by Matthew Weier O'Phinney at http://weierophinney.net/matthew/)
76.
77.
78. protected function _initRestroutes() { $this->bootstrap('frontController'); $frontController = Zend_Controller_Front::getInstance(); $router = $frontController->getRouter(); $restRoute = new Zend_Rest_Route($frontController,array(),array('rest')); $router->addRoute('rest', $restRoute); }
79. REST with ZF Zend_Rest_Route detects HTTP verbs like POST and points them to a predefined list of corresponding URI end points. Our example code will result in behaviour like this: URI Module_Controller action GET /rest/quote/ Rest_QuoteController indexAction() GET /rest/quote/:id Rest_QuoteController getAction() POST /rest/quote Rest_QuoteController postAction() PUT /rest/quote/:id Rest_QuoteController putAction() DELETE /rest/quote/:id Rest_QuoteController deleteAction() These additional end points are supported too, as a workaround for environments where PUT or DELETE are blocked by a firewall for example: URI Module_Controller action POST /rest/quote/:id?_method=PUT Rest_QuoteController putAction() POST /rest/quote/:id?_method=DELETE Rest_QuoteController deleteAction() And that counts for every controller in the module named 'rest', as they are all considered RESTful.
80. REST with ZF SERVER -> code example: route + controller CLIENT Zend_Rest_Client -> no $client = new Zend_Http_Client('http://presentation-zfws/rest/index/2'); $response = $client->request(); echo $response->getBody();
81. REST – Response codes Error and response codes ! For example: 200 OK Everything allright 201 Created The object or resources is created 204 Empty body Resource is deleted 400 Bad Request Request is not understood or was malformed. Explanation may be available in the body . 401 Forbidden Permission denied. / credentials invalid 404 Not Found Resource could not be found. 409 Conflict/Duplicate Resource already exists 405 Method Not Allowed The action (POST, PUT, ...) is not allowed on the resource 410 Gone Resource does not/no longer exist 500 Internal server error Something went wrong on the server side 501 Not Implemented The action is not available.