RESTful API development in Laravel 4 - Christopher Pecoraro

44
RESTful API development in Laravel 4 Christopher Pecoraro phpDay 2014 Verona, Italy May 16th-17th, 2014

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 of RESTful API development in Laravel 4 - Christopher Pecoraro

Page 1: RESTful API development in Laravel 4 - Christopher Pecoraro

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

I was born in 1976 in Pittsburgh, Pennsylvania, USA.

Page 3: RESTful API development in Laravel 4 - Christopher Pecoraro

I have been living in Mondello, Sicily since 2009.

Page 4: RESTful API development in Laravel 4 - Christopher Pecoraro

Two households both alike in dignity…

Page 5: RESTful API development in Laravel 4 - Christopher Pecoraro

Triangular...

Page 6: RESTful API development in Laravel 4 - Christopher Pecoraro

Triangular...

Page 7: RESTful API development in Laravel 4 - Christopher Pecoraro

...with a fountain...

Page 8: RESTful API development in Laravel 4 - Christopher Pecoraro

...with a fountain...

Page 9: RESTful API development in Laravel 4 - Christopher Pecoraro

...and a temple.

Page 10: RESTful API development in Laravel 4 - Christopher Pecoraro

...and a temple.

Page 11: RESTful API development in Laravel 4 - Christopher Pecoraro

the first integrated viral media company

Page 12: RESTful API development in Laravel 4 - Christopher Pecoraro

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

Worldwide growth in Google search from January 2012 - Present

Page 14: RESTful API development in Laravel 4 - Christopher Pecoraro

Laravel takes advantage of the Facade pattern:

Input::get('foo');

Page 15: RESTful API development in Laravel 4 - Christopher Pecoraro

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

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

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

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

Relevant Terms:● Eloquent ORM

● Artisan CLI

● Resource Controller

Page 20: RESTful API development in Laravel 4 - Christopher Pecoraro

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

Case study: blog post tagging system

Database Tables:

post_tag tagsposts

Page 22: RESTful API development in Laravel 4 - Christopher Pecoraro

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

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

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

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

Post Model<?php

Class Post extends Eloquent {

}

Page 27: RESTful API development in Laravel 4 - Christopher Pecoraro

Tag Model<?php

Class Tag extends Eloquent {

}

Page 28: RESTful API development in Laravel 4 - Christopher Pecoraro

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

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

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

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

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

$ 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

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

<?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

// 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

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

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

public function show($id)

{

return Post::with('tags') ->remember(240)->find($id);

}

Need caching?

Page 40: RESTful API development in Laravel 4 - Christopher Pecoraro

// 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

// 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

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

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

Grazie mille -- Thank you very much