Date post: | 27-Aug-2014 |
Category: |
Software |
Upload: | christopher-pecoraro |
View: | 1,875 times |
Download: | 0 times |
RESTful API development in Laravel 4
Christopher Pecoraro
phpDay 2014Verona, Italy
May 16th-17th, 2014
I was born in 1976 in Pittsburgh, Pennsylvania, USA.
I have been living in Mondello, Sicily since 2009.
Two households both alike in dignity…
Triangular...
Triangular...
...with a fountain...
...with a fountain...
...and a temple.
...and a temple.
the first integrated viral media company
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…
Worldwide growth in Google search from January 2012 - Present
Laravel takes advantage of the Facade pattern:
Input::get('foo');
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
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.
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)
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.)
Relevant Terms:● Eloquent ORM
● Artisan CLI
● Resource Controller
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.
Case study: blog post tagging system
Database Tables:
post_tag tagsposts
Database structureposts table:id mediumint autoincrement unsigned
title varchar(1000)
body text
tags table:id mediumint autoincrement unsigned
name varchar(1000)
Database structurepost_tag: (pivot table)id mediumint autoincrement unsigned
post_id mediumint
tag_id mediumint
Case study: blog post tagging system
Database Tables:
post_tag tagsposts
Eloquent Models use convention over configuration:
Post Tag
Laravel manages singular/plural, so be careful:
echo str_plural('mouse');
mice
echo str_singular('media');
medium
echo str_plural('prognosis');
prognoses
Post Model<?php
Class Post extends Eloquent {
}
Tag Model<?php
Class Tag extends Eloquent {
}
Post Model with relation<?php
Class Post extends Eloquent {
public function tags()
{
return $this->belongsToMany('Tag');
}
}
Eloquent methods$post = Post::find(1);
$tags = $post->tags;
$tags:
{
"tags" : [{"id": "10", "name": "Sicily"},{"id": "16", "name":
"Tourism"}]
}
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
CRUDLaction: HTTP verb: path:
Create POST /postsRead GET /posts/idUpdate PUT/PATCH /posts/idDelete DELETE /posts/idList GET /posts
Artisan CLI
Laravel’s command line interface tool that performs basic development tasks.
● Perform migrations
● Create resource controllers
● Other great tasks
$ php artisan controller:make PostsController
Let’s create our controller for 'Post' model:
/app/controllers/PostsController.php
Route::resource('posts',
'PostsController');
Let’s add the route for the 'Post' Controller to the routes.php file:
/app/controllers/PostsController.php
<?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:
// 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”
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": "....."
}
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”
public function show($id)
{
return Post::with('tags') ->remember(240)->find($id);
}
Need caching?
// 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
// Delete DELETE http://api.domain.com/posts/{id}
public function destroy($id) {
$post = Post::find($id);
$post->delete();
return Response::make('',204);
}
CRUDL
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" : "....."},
...
]
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
Grazie mille -- Thank you very much