RESTful API development in Laravel 4 - Christopher Pecoraro

16,968 views

Published on

RESTful APIs are useful when developing a modern web application since they allow for multiple possibilities for collaboration with third-party software as well as multiple types of front-ends, such as mobile apps and HTML5 web applications. The creation of RESTful API’s is facilitated using Laravel 4, a PHP 5.3 Framework that is rapidly growing in popularity. Laravel’s features such as the facade pattern allow for easy-to-read code and expressive ORM statements.

2 Comments
36 Likes
Statistics
Notes
No Downloads
Views
Total views
16,968
On SlideShare
0
From Embeds
0
Number of Embeds
505
Actions
Shares
0
Downloads
354
Comments
2
Likes
36
Embeds 0
No embeds

No notes for slide

RESTful API development in Laravel 4 - Christopher Pecoraro

  1. 1. RESTful API development in Laravel 4 Christopher Pecoraro phpDay 2014 Verona, Italy May 16th-17th, 2014
  2. 2. I was born in 1976 in Pittsburgh, Pennsylvania, USA.
  3. 3. I have been living in Mondello, Sicily since 2009.
  4. 4. Two households both alike in dignity…
  5. 5. Triangular...
  6. 6. Triangular...
  7. 7. ...with a fountain...
  8. 8. ...with a fountain...
  9. 9. ...and a temple.
  10. 10. ...and a temple.
  11. 11. the first integrated viral media company
  12. 12. Laravel A PHP 5.3 PHP 5.4 framework Laravel Version Minimum PHP version required 4.0 5.3 4.1 5.3 4.2 5.4 Upgrade to at least PHP 5.4, if not PHP 5.5: Trust Rasmus and Lorna Jane In fair Verona, where we lay our scene…
  13. 13. Worldwide growth in Google search from January 2012 - Present
  14. 14. Laravel takes advantage of the Facade pattern: Input::get('foo');
  15. 15. Installing Laravel ● Download: http://laravel.com/laravel.phar ● sudo apt-get install php5-mcrypt ● Rename laravel.phar to laravel and move it to /usr/local/bin ● laravel new blog ("blog" is the project name in this case.) ● chmod -R 777 app/storage
  16. 16. Installing Laravel Other options: ● Download a Laravel Vagrant box. ● Use forge.laravel.com: ○ Deploy a box with PHP 5.5, Hack (Beta), and HHVM ○ Tuned specifically for Laravel on Digital Ocean, RackSpace, etc.
  17. 17. Laravel has an expressive syntax, easing common tasks such as: ● authentication ● routing ● sessions ● caching Laravel combines aspects of other frameworks such as: ● Ruby on Rails (active record) ● ASP.NET MVC Laravel features: ● inversion of control (IoC) container ● database migration ● unit testing support (PHPUnit)
  18. 18. Laravel application structure Important files and directories: /app /models /controllers /views routes.php filters.php /public (assets such as javascript, css files) /config (settings for database drivers, smtp, etc.)
  19. 19. Relevant Terms: ● Eloquent ORM ● Artisan CLI ● Resource Controller
  20. 20. Eloquent ORM Acts as the M (model) part of MVC ● It allows developers to use an object-oriented approach. ● Interacts with database tables by representing them as models.
  21. 21. Case study: blog post tagging system Database Tables: post_tag tagsposts
  22. 22. Database structure posts table: id mediumint autoincrement unsigned title varchar(1000) body text tags table: id mediumint autoincrement unsigned name varchar(1000)
  23. 23. Database structure post_tag: (pivot table) id mediumint autoincrement unsigned post_id mediumint tag_id mediumint
  24. 24. Case study: blog post tagging system Database Tables: post_tag tagsposts Eloquent Models use convention over configuration: Post Tag
  25. 25. Laravel manages singular/plural, so be careful: echo str_plural('mouse'); mice echo str_singular('media'); medium echo str_plural('prognosis'); prognoses
  26. 26. Post Model <?php Class Post extends Eloquent { }
  27. 27. Tag Model <?php Class Tag extends Eloquent { }
  28. 28. Post Model with relation <?php Class Post extends Eloquent { public function tags() { return $this->belongsToMany('Tag'); } }
  29. 29. Eloquent methods $post = Post::find(1); $tags = $post->tags; $tags: { "tags" : [{"id": "10", "name": "Sicily"},{"id": "16", "name": "Tourism"}] }
  30. 30. Resource Controller Represents the C part of the MVC ● Allows us to easily create RESTful APIs using models ● Handles routing for GET, PUT/PATCH, POST, DELETE
  31. 31. CRUDL action: HTTP verb: path: Create POST /posts Read GET /posts/id Update PUT/PATCH /posts/id Delete DELETE /posts/id List GET /posts
  32. 32. Artisan CLI Laravel’s command line interface tool that performs basic development tasks. ● Perform migrations ● Create resource controllers ● Other great tasks
  33. 33. $ php artisan controller:make PostsController Let’s create our controller for 'Post' model: /app/controllers/PostsController.php
  34. 34. Route::resource('posts', 'PostsController'); Let’s add the route for the 'Post' Controller to the routes.php file: /app/controllers/PostsController.php
  35. 35. <?php PostsController extends BaseController { public function index(){ } public function store(){ } public function show($id){ } public function update($id){ } public function destroy($id){ } } Here’s what gets created:
  36. 36. // Create POST http://api.domain.com/posts public function store() { $post = new Post; $post->title = Input::get('title'); $post->body = Input::get('body'); $post->save(); return Response::make(['id'=>$post->id],201); } CRUDL “New post”
  37. 37. CRUDL “Post: find id” // Read GET http://api.domain.com/posts/{id} public function show($id) { return Post::find($id); } returns: { "id": 23, "title": "Nice beaches In Italy", "body": "....." }
  38. 38. public function show($id) { return Post::with('tags')->find($id); } returns: { "id" : "23", "title" : "Nice beaches In Italy", "body" : ".....", "tags" : [{ "name": "Sicily" }...] } CRUDL “Post: find id with tags”
  39. 39. public function show($id) { return Post::with('tags') ->remember(240) ->find($id); } Need caching?
  40. 40. // Create POST // http://api.domain.com/posts public function store() { $post = new Post; $post->title = Input::get('title'); $post->body = Input::get('body'); $post->save(); return Response::make(['id'=>$post->id],201); } // Update PUT/PATCH // http://api.domain.com/posts/{id} public function update($id) { $post = Post::find($id); $post->title = Input::get('title'); $post->body = Input::get('body'); $post->save(); return Response::make($post, 200); } CRUDL
  41. 41. // Delete DELETE http://api.domain.com/posts/{id} public function destroy($id) { $post = Post::find($id); $post->delete(); return Response::make('',204); } CRUDL
  42. 42. CRUDL // List GET http://api.domain.com/posts public function index() { return Post::all(); } returns: [{ "id" : "23", "title" : "Nice beaches In Italy", "body" : "....."}, { "id" : "24", "title" : "Visiting Erice", "body" : "....."}, { "id" : "25", "title" : "Beautiful Taormina", "body" : "....."}, ... ]
  43. 43. Need Authentication? Route::resource('posts', 'PostController') ->before('auth'); filters.php: Route::filter('auth', function() { if (Auth::guest()){ return Response::make('', 401); } }); Need OAuth2? lucadegasperi/oauth2-server-laravel
  44. 44. Grazie mille -- Thank you very much

×