Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
REST API & CakePHP
Anuchit Chalothorn
anuchit@redlinesoft.net
Agenda
● REST
● Route
● Resource Mapping
REST
a style of software architecture for distributed
systems such as the WWW. The REST
language uses nouns and verbs, and...
Concept
● the base URI for the web service, such as
http://example.com/resources/
● the Internet media type of the data
su...
Example URI
● http://example.org/user/
● http://example.org/user/anuchit
● http://search.twitter.com/search.json?q=xxx
Example methods
Resource GET PUT POST DELETE
http://example.org/user list collection replace create delete
http://example....
Simple Diagram
Requester Provider
GET /user/anuchit HTTP/1.1
200 with some data
No "official" standard
There is no "official" standard for RESTful web
services, This is because REST is an
architectural ...
Shortcut - Web Services design
● Choose method old style, new style
● Look around an eco-system
● Who'll using your servic...
REST & CakePHP
The fastest way to get up and running with
REST is to add a few lines to your routes.php
file The Router ob...
Route
If we wanted to allow REST access to a recipe
database, we’d do something like this:
//In app/Config/routes.php...
R...
HTTP REQUEST Methods
HTTP Format URL.Format Controller action invoked
GET /recipes.format RecipesController::index()
POST ...
REST & CakePHP
The fastest way to get up and running with
REST is to add a few lines to your routes.php
file The Router ob...
Controller (1)
A basic controller might look something like this
// Controller/RecipesController.php
class RecipesControll...
Controller (2)
public function view($id) {
$recipe = $this->Recipe->findById($id);
$this->set(array(
'recipe' => $recipe,
...
Controller (3)
public function edit($id) {
$this->Recipe->id = $id;
if ($this->Recipe->save($this->request->data)) {
$mess...
Controller (4)
public function delete($id) {
if ($this->Recipe->delete($id)) {
$message = 'Deleted';
} else {
$message = '...
View
Since we’ve added a call to Router::
parseExtensions(), the CakePHP router is
already primed to serve up different vi...
Modify View (1)
If we wanted to modify the data before it is
converted into XML we should not define the
_serialize view v...
Modify View (2)
// app/View/Recipes/xml/index.ctp
// Do some formatting and manipulation on
// the $recipes array.
$xml = ...
Resource Mapping
If the default REST routes don’t work for your
application, you can modify them using Router::
resourceMa...
Thank You
Upcoming SlideShare
Loading in …5
×

REST API with CakePHP

20,316 views

Published on

Published in: Technology, Self Improvement

REST API with CakePHP

  1. 1. REST API & CakePHP Anuchit Chalothorn anuchit@redlinesoft.net
  2. 2. Agenda ● REST ● Route ● Resource Mapping
  3. 3. REST a style of software architecture for distributed systems such as the WWW. The REST language uses nouns and verbs, and has an emphasis on readability. Unlike SOAP, REST does not require a message header to and from a service provider.
  4. 4. Concept ● the base URI for the web service, such as http://example.com/resources/ ● the Internet media type of the data supported by the web service. ● the set of operations supported by the web service using HTTP methods (e.g., GET, PUT, POST, or DELETE). ● The API must be hypertext driven.
  5. 5. Example URI ● http://example.org/user/ ● http://example.org/user/anuchit ● http://search.twitter.com/search.json?q=xxx
  6. 6. Example methods Resource GET PUT POST DELETE http://example.org/user list collection replace create delete http://example.org/user/rose list data replace/ create ? / create delete
  7. 7. Simple Diagram Requester Provider GET /user/anuchit HTTP/1.1 200 with some data
  8. 8. No "official" standard There is no "official" standard for RESTful web services, This is because REST is an architectural style, unlike SOAP, which is a protocol.
  9. 9. Shortcut - Web Services design ● Choose method old style, new style ● Look around an eco-system ● Who'll using your services ● How to implementation ● Design and document
  10. 10. REST & CakePHP The fastest way to get up and running with REST is to add a few lines to your routes.php file The Router object features a method called mapResources(), that is used to set up a number of default routes for REST access to your controllers.
  11. 11. Route If we wanted to allow REST access to a recipe database, we’d do something like this: //In app/Config/routes.php... Router::mapResources('recipes'); Router::parseExtensions();
  12. 12. HTTP REQUEST Methods HTTP Format URL.Format Controller action invoked GET /recipes.format RecipesController::index() POST /recipes.format RecipesController::add() PUT /recipes/123.format RecipesController::edit(123) DELETE /recipes/123.format RecipesController::delete(123) POST /recipes/123.format RecipesController::edit(123)
  13. 13. REST & CakePHP The fastest way to get up and running with REST is to add a few lines to your routes.php file The Router object features a method called mapResources(), that is used to set up a number of default routes for REST access to your controllers.
  14. 14. Controller (1) A basic controller might look something like this // Controller/RecipesController.php class RecipesController extends AppController { public $components = array('RequestHandler'); public function index() { $recipes = $this->Recipe->find('all'); $this->set(array( 'recipes' => $recipes, '_serialize' => array('recipes') )); }
  15. 15. Controller (2) public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); }
  16. 16. Controller (3) public function edit($id) { $this->Recipe->id = $id; if ($this->Recipe->save($this->request->data)) { $message = 'Saved'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); }
  17. 17. Controller (4) public function delete($id) { if ($this->Recipe->delete($id)) { $message = 'Deleted'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); }
  18. 18. View Since we’ve added a call to Router:: parseExtensions(), the CakePHP router is already primed to serve up different views based on different kinds of requests.
  19. 19. Modify View (1) If we wanted to modify the data before it is converted into XML we should not define the _serialize view variable and instead use view files. We place the REST views for our RecipesController inside app/View/recipes/xml.
  20. 20. Modify View (2) // app/View/Recipes/xml/index.ctp // Do some formatting and manipulation on // the $recipes array. $xml = Xml::fromArray(array('response' => $recipes)); echo $xml->asXML();
  21. 21. Resource Mapping If the default REST routes don’t work for your application, you can modify them using Router:: resourceMap(). Router::resourceMap(array( array('action' => 'index', 'method' => 'GET', 'id' => false), array('action' => 'view', 'method' => 'GET', 'id' => true), array('action' => 'add', 'method' => 'POST', 'id' => false), array('action' => 'edit', 'method' => 'PUT', 'id' => true), array('action' => 'delete', 'method' => 'DELETE', 'id' => true), array('action' => 'update', 'method' => 'POST', 'id' => true) ));
  22. 22. Thank You

×