Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani

40,621 views

Published on

Explains how to use the Slim PHP micro-framework to quickly deploy a REST API. Includes sample code to implement common API requirements, such as authentication, request logging and multi-format support.

Published in: Technology
5 Comments
66 Likes
Statistics
Notes
  • Here is a detailed article on how you can create a Rest API using Slim framework for beginners. https://www.cloudways.com/blog/simple-rest-api-with-slim-micro-framework/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • That's the idea that came to my mind when I went to sleep last night, Slim and redbean for the api, Angularjs for the frontend, i'm happy. great Slide, thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hey I am new for Slim framework and I want to implement login system with authentication using in my project, Is there any good example or resource for the same. I am using Eloquent ORM in my project. Please give me some suggestion for this. Thanks.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I created a project named RedSlim which is a pre-configured development environemnt using Slim, Twig, Redbean and Bootstrap. The code is hosted in github (https://github.com/vanting/RedSlim), and you can deploy it to the pagodabox cloud (the slim hosting) within a minute.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • i don't know what to think about repurposing PUT for updating instead of putting resources/files somewhere. its elegant as you do not need a special parameter in a POST request, on the other hand it is confusing :/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
40,621
On SlideShare
0
From Embeds
0
Number of Embeds
601
Actions
Shares
0
Downloads
0
Comments
5
Likes
66
Embeds 0
No embeds

No notes for slide
  • Typically less than 200-300 KB in size. Slim is ~200 KB. Based on ideas promulgated by Sinatra (Ruby). Reduces overall size of project. Most projects use 5% of a full-stack framework like Zend, Symfony, etc.
  • The modified object is then saved back to the database, and a JSON representation returned to the caller with a 200 server response code. In the event that the identifier provided does not match an existing resource, a custom exception is thrown and a 404 server error response sent back to the client.
  • The response to a successful DELETE request can be a 200 (OK) with the status in the response body, or a 204 (No Content) with an empty response body
  • Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani

    1. 1. Creating REST Applicationswith the Slim Micro-Framework Vikram Vaswani October 2012
    2. 2. slimadj. slim·mer, slim·mest 1. Small in girth or thickness in proportion to height or length; slender. 2. Small in quantity or amount; meager: slim chances of success. From www.thefreedictionary.com
    3. 3. slimPHP micro framework that helps youquickly write simple yet powerful webapplications and APIs. From www.slimframework.com
    4. 4. Micro-Frameworks in 20 18 Words● Lightweight● Short learning curve● Flexible● Single-purpose● Good for ● Small, static websites ● API development and testing ● Rapid prototyping
    5. 5. PHP Micro-Frameworks● Silex ● Phraw● Limonade ● Photon● GluePHP ● Fitzgerald● FlightPHP ● phpMF● Fat-Free Framework ● Swiftlet● MicroMVC ● Tachyon● Bullet ● Pupcake● Bento ● Relax● Yolo ● Lime● Fluphy ● Shield● Tonic ● Hydra● Phlyty ● Gum
    6. 6. Entrée
    7. 7. Introducing Slim "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs." From www.slimframework.com
    8. 8. Key Features● Powerful router ● Standard and custom HTTP methods ● Route parameters with wildcards and conditions ● Route middleware● Template rendering with custom views● Flash messages● Secure cookies with AES-256 encryption
    9. 9. Key Features● HTTP caching● Logging with custom log writers● Error handling and debugging● Middleware architecture● Simple configuration● Extensive documentation● Active, enthusiastic community● Licensed under the MIT Public License
    10. 10. First Taste<?php// loadrequire Slim/Slim.php;SlimSlim::registerAutoloader();// initialize$app = new SlimSlim();// map routes$app->get(/eat/:food, function ($food) { echo "Yum! That $food looks fantastic!";});// ...and were off!$app->run();
    11. 11. First Taste
    12. 12. Main Course
    13. 13. Slim + REST● Define URI routes, parameters and HTTP methods ● GET / POST / PUT / DELETE ● /my/api/endpoint/:parameter● Map routes to callbacks ● Accept and validate input parameters from request object ● Perform custom processing within callback ● Produce and return response object
    14. 14. Example: REST Resources So urce : W ikipe dia e n. wikipe dia. o rg /wiki/Pe rio dic_ table
    15. 15. REST APIGET /api/v1/elements Retrieve all elements from the periodic tableGET /api/v1/elements/1 Retrieve element #1 (hydrogen)POST /api/v1/elements Create new elementPUT /api/v1/elements/28 Update element #28 (nickel)DELETE /api/v1/elements/8 Delete element #8 (oxygen)
    16. 16. Resource Representation (MySQL)CREATE TABLE IF NOT EXISTS `elements` ( `id` int(11) NOT NULL AUTO_INCREMENT, `num` int(11) NOT NULL, `name` varchar(255) NOT NULL, `symbol` char(2) NOT NULL, `weight` float NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;INSERT INTO `elements` (`id`, `num`, `name`, `symbol`,`weight`) VALUES(1, 1, Hydrogen, H, 1.008);INSERT INTO `elements` (`id`, `num`, `name`, `symbol`,`weight`) VALUES(2, 2, Helium, He, 4.0026);
    17. 17. Resource Representation (JSON) { "id":"1", "num":"1", "name":"Hydrogen", "symbol":"H", "weight":"1.008" },{ "id":"2", "num":"2", "name":"Helium", "symbol":"He", "weight":"4.0026" }
    18. 18. RedBeanPHP<?php// loadrequire RedBean/rb.php;// set up database connectionR::setup(mysql:host=localhost;dbname=appdata,user,pass);R::freeze(true);// get all records$elements = R::find(elements);
    19. 19. RedBeanPHP<?php// get a single record$element = R::findOne(elements, id=?,array($id));// update element record$element->name = foo// save modified recordR::store($element);
    20. 20. REST API Implementation<?php$app->get(/api/v1/elements, function () { ... }$app->get(/api/v1/elements/:id, function ($id) { ... }$app->post(/api/v1/elements, function ($id) { ... }$app->put(/api/v1/elements/:id, function ($id) { ... }$app->delete(/api/v1/elements/:id, function ($id) { ... }
    21. 21. GET Request Handler<?php// handle GET requests for /api/v1/elements$app->get(/api/v1/elements, function () use ($app) { // get all elements $elements = R::find(elements); // create JSON response $app->response()->header(Content-Type,application/json); echo json_encode(R::exportAll($elements));});
    22. 22. GET Response
    23. 23. GET Request Handler<?php// handle GET requests for /api/v1/elements/*$app->get(/api/v1/elements/:id, function ($id) use ($app) { // get element by id $article = R::findOne(elements, id=?, array($id)); // create JSON response if element found // else send 404 server error if ($article) { $app->response()->header(Content-Type, application/json); echo json_encode(R::exportAll($article)); } else { $app->response()->status(404); }});
    24. 24. GET Response
    25. 25. In The News Recently... So urce : Live Scie nce www. live scie nce . co m/20 6 9 8 -e le me nts-pe rio dic-table -fle ro vium-live rmo rium. html
    26. 26. POST Request Handler<?php// handle POST requests for /api/v1/elements/*$app->post(/api/v1/elements, function () use ($app) { // get request body, decode into PHP object $request = $app->request(); $body = $request->getBody(); $input = json_decode($body); // create and save element record $element = R::dispense(elements); $element->num = (string)$input->num; // do same for other attributes R::store($element); // create and send JSON response $app->response()->status(201); $app->response()->header(Content-Type, application/json); echo json_encode(R::exportAll($element));});
    27. 27. POST Request
    28. 28. POST Response
    29. 29. PUT Request Handler<?php// handle PUT requests for /api/v1/elements/*$app->put(/api/v1/elements/:id, function ($id) use ($app) { // get request body, decode into PHP object $request = $app->request(); $body = $request->getBody(); $input = json_decode($body); // retrieve specified element record // save modified record, create and send JSON response $element = R::findOne(elements, id=?, array($id)); if ($element) { $element->num = (string)$input->num; // do same for other attributes R::store($element); $app->response()->header(Content-Type, application/json); echo json_encode(R::exportAll($element)); } else { $app->response()->status(404); }});
    30. 30. PUT Request
    31. 31. PUT Response
    32. 32. DELETE Request Handler<?php// handle DELETE requests for /api/v1/elements$app->delete(/api/v1/elements/:id, function ($id) use ($app) { $request = $app->request(); $element = R::findOne(elements, id=?, array($id)); if ($element) { R::trash($element); $app->response()->status(204); } else { $app->response()->status(404); }});
    33. 33. DELETE Response
    34. 34. Accompaniments
    35. 35. Common Requirements● Authentication● Parameter validation● Request logging● Multi-format support
    36. 36. Common Requirements● Authentication● Parameter validation● Request logging● Multi-format support
    37. 37. Middleware"The purpose of middleware is to inspect, analyze, or modify the application environment, request, and response before and/or after the Slim application is invoked." From docs.slimframework.com
    38. 38. Middleware Use Cases● Authentication/authorization● Request pre-processing● Response post-processing● Filter chaining● Logging
    39. 39. Example: API Authentication<?php// route middleware for simple API authenticationfunction authenticate(SlimRoute $route) { $app = SlimSlim::getInstance(); $uid = $app->getEncryptedCookie(uid); $key = $app->getEncryptedCookie(key); if (validateUserKey($uid, $key) === false) { $app->halt(401); }}// add API authentication for GET requests$app->get(/api/v1/elements, authenticate, function () use ($app) { // GET handler code here});
    40. 40. Response to Unauthenticated APIRequest
    41. 41. Common Requirements● Authentication● Parameter validation● Request logging● Multi-format support
    42. 42. Example: API Parameter Validation● Optional and mandatory route parameters:<?php$app->get(/api/v1/elements(/:symbol), function() use ($app) { ... };● Route conditions:<?php$app->get(/api/v1/elements/:id, function () use($app) { ... }->conditions(array(id => [0-9]{1,}));
    43. 43. Common Requirements● Authentication● Parameter validation● Request logging● Multi-format support
    44. 44. Request Object<?php// get request object$request = $app->request();// get request headers as associative array$headers = $request->headers();// get request path$type = $request->getPathInfo()// get request IP address and host$body = $request->getIp() . , . $request->getHost();
    45. 45. Example: API Request Logging<?php// initialize logger$app = new SlimSlim(array( log.enabled => true, log.writer => new FileLogWriter()));class FileLogWriter { public function write($message) { file_put_contents(my.log, $message . PHP_EOL,FILE_APPEND); }}
    46. 46. Example: Request Logging<?php// handle GET requests for /api/v1/elements$app->get(/api/v1/elements, function () use ($app) { // get request headers and log message $request = $app->request(); $message = sprintf("%s [%s] "%s %s"", $request->getIp(), time(), $request->getMethod(), $request->getPathInfo() ); $app->getLog()->info($message); // handler code}
    47. 47. Common Requirements● Authentication● Parameter validation● Request logging● Multi-format support
    48. 48. Request Object<?php// get request object$request = $app->request();// get request parameters as associative array$headers = $request->params();// get cookies$headers = $request->cookies();// get request content type$type = $request->getMediaType()// get raw request body$body = $request->getBody();
    49. 49. Example: API Multi-Format Support<?php$app->get(/api/v1/elements, function () use ($app) { // check request content type, send XML or JSON response $mediaType = $app->request()->getMediaType(); if ($mediaType == application/xml) { $app->response()->header(Content-Type, application/xml); $xml = new SimpleXMLElement(<root/>); foreach (R::exportAll($elements) as $r) { $item = $xml->addChild(item); $item->addChild(id, $r[id]); // do same for other attributes } echo $xml->asXml(); } else if (($mediaType == application/json)) { $app->response()->header(Content-Type, application/json); echo json_encode(R::exportAll($elements)); }});
    50. 50. GET Response (XML)
    51. 51. POST Request (XML)
    52. 52. POST Response (XML)
    53. 53. Dessert
    54. 54. Performance Put simply: F = ma
    55. 55. Or, To Put It Another Way... “ If everything seems under control, youre not going fast enough.” - Mario Andretti So urce : Go o dre ads. co m http: //www. g o o dre ads. co m/autho r/quo te s/21 1 56 9 4. M _ A tti ario ndre
    56. 56. WeightFramework Self time (Milliseconds) Sum of callsSlim 2.0.0 1,008 3,612Zend Framework 1.11 2,917 4,616Agavi 1.0.1 13,885 25,988 30000 25000 20000 Slim 15000 Zend Framework Agavi 10000 5000 0 Sum of Calls
    57. 57. Requests Per SecondFramework Throughput (Requests/ Second)Slim 2.0.0 29.43Zend Framework 1.11 15.10Agavi 1.0.1 2.44 35 30 25 20 Slim Zend Framework 15 Agavi 10 5 0 Throughput
    58. 58. Minimum Request TimeFramework M R in. equest Time (Milliseconds)Slim 2.0.0 53Zend Framework 1.11 123Agavi 1.0.1 412 450 400 350 300 250 Slim Zend Framework 200 Agavi 150 100 50 0 Min. Request Time
    59. 59. "Lies, Damned Lies and Statistics"Test data based on informal testing● Apache JMeter ● 300 concurrent requests, averaged over 3 test runs● Dual-core Dell server @ 2.4 GhZ● Apache 2.2, PHP 5.3, Xdebug 2.2 and MySQL 5.1● Unoptimized applications; default framework settings● Your mileage may (and probably will) vary!
    60. 60. Resources● Usage docs: docs.slimframework.com● API docs: dev.slimframework.com/phpdocs● Source code: github.com/codeguy/Slim● Forums: help.slimframework.com● News: www.slimframework.com/news● Twitter: @slimphp
    61. 61. Questions?
    62. 62. Contact InformationEmail:● vikram-vaswani.in/contactWeb:● www.melonfire.com● vikram-vaswani.inSocial networks:● plus.google.com/100028886433648406825

    ×