restful api development in laravel 4 - christopher pecoraro
DESCRIPTION
RESTful APIs are useful when developing a modern web application since it allows 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.TRANSCRIPT
![Page 1: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/1.jpg)
RESTful API development in Laravel 4
Christopher Pecoraro
phpDay 2014Verona, Italy
May 16th-17th, 2014
![Page 2: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/2.jpg)
I was born in 1976 in Pittsburgh, Pennsylvania, USA.
![Page 3: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/3.jpg)
I have been living in Mondello, Sicily since 2009.
![Page 4: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/4.jpg)
Two households both alike in dignity…
![Page 5: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/5.jpg)
Triangular...
![Page 6: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/6.jpg)
Triangular...
![Page 7: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/7.jpg)
...with a fountain...
![Page 8: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/8.jpg)
...with a fountain...
![Page 9: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/9.jpg)
...and a temple.
![Page 10: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/10.jpg)
...and a temple.
![Page 11: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/11.jpg)
the first integrated viral media company
![Page 12: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/12.jpg)
Laravel A PHP 5.3 PHP 5.4 framework
Laravel Version Minimum PHP version required4.0 5.34.1 5.34.2 5.4Upgrade to at least PHP 5.4, if not PHP 5.5: Trust Rasmus and Lorna Jane
In fair Verona, where we lay our scene…
![Page 13: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/13.jpg)
Worldwide growth in Google search from January 2012 - Present
![Page 14: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/14.jpg)
Laravel takes advantage of the Facade pattern:
Input::get('foo');
![Page 15: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/15.jpg)
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
![Page 16: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/16.jpg)
Installing LaravelOther 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.
![Page 17: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/17.jpg)
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)
![Page 18: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/18.jpg)
Laravel application structureImportant 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.)
![Page 19: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/19.jpg)
Relevant Terms:● Eloquent ORM
● Artisan CLI
● Resource Controller
![Page 20: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/20.jpg)
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.
![Page 21: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/21.jpg)
Case study: blog post tagging system
Database Tables:
post_tag tagsposts
![Page 22: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/22.jpg)
Database structureposts table:id mediumint autoincrement unsigned
title varchar(1000)
body text
tags table:id mediumint autoincrement unsigned
name varchar(1000)
![Page 23: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/23.jpg)
Database structurepost_tag: (pivot table)id mediumint autoincrement unsigned
post_id mediumint
tag_id mediumint
![Page 24: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/24.jpg)
Case study: blog post tagging system
Database Tables:
post_tag tagsposts
Eloquent Models use convention over configuration:
Post Tag
![Page 25: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/25.jpg)
Laravel manages singular/plural, so be careful:
echo str_plural('mouse');
mice
echo str_singular('media');
medium
echo str_plural('prognosis');
prognoses
![Page 26: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/26.jpg)
Post Model<?php
Class Post extends Eloquent {
}
![Page 27: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/27.jpg)
Tag Model<?php
Class Tag extends Eloquent {
}
![Page 28: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/28.jpg)
Post Model with relation<?php
Class Post extends Eloquent {
public function tags()
{
return $this->belongsToMany('Tag');
}
}
![Page 29: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/29.jpg)
Eloquent methods$post = Post::find(1);
$tags = $post->tags;
$tags:
{
"tags" : [{"id": "10", "name": "Sicily"},{"id": "16", "name":
"Tourism"}]
}
![Page 30: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/30.jpg)
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
![Page 31: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/31.jpg)
CRUDLaction: HTTP verb: path:
Create POST /postsRead GET /posts/idUpdate PUT/PATCH /posts/idDelete DELETE /posts/idList GET /posts
![Page 32: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/32.jpg)
Artisan CLI
Laravel’s command line interface tool that performs basic development tasks.
● Perform migrations
● Create resource controllers
● Other great tasks
![Page 33: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/33.jpg)
$ php artisan controller:make PostsController
Let’s create our controller for 'Post' model:
/app/controllers/PostsController.php
![Page 34: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/34.jpg)
Route::resource('posts',
'PostsController');
Let’s add the route for the 'Post' Controller to the routes.php file:
/app/controllers/PostsController.php
![Page 35: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/35.jpg)
<?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:
![Page 36: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/36.jpg)
// 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”
![Page 37: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/37.jpg)
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": "....."
}
![Page 38: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/38.jpg)
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”
![Page 39: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/39.jpg)
public function show($id)
{
return Post::with('tags') ->remember(240)->find($id);
}
Need caching?
![Page 40: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/40.jpg)
// 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
![Page 41: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/41.jpg)
// Delete DELETE http://api.domain.com/posts/{id}
public function destroy($id) {
$post = Post::find($id);
$post->delete();
return Response::make('',204);
}
CRUDL
![Page 42: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/42.jpg)
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" : "....."},
...
]
![Page 43: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/43.jpg)
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
![Page 44: RESTful API development in Laravel 4 - Christopher Pecoraro](https://reader033.vdocument.in/reader033/viewer/2022042606/53fe09618d7f72db2d8b457e/html5/thumbnails/44.jpg)
Grazie mille -- Thank you very much