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.
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…
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. 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. 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. 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.)
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. Case study: blog post tagging system
Database Tables:
post_tag tagsposts
22. Database structure
posts table:
id mediumint autoincrement unsigned
title varchar(1000)
body text
tags table:
id mediumint autoincrement unsigned
name varchar(1000)
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. 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. Artisan CLI
Laravel’s command line interface tool that performs basic
development tasks.
● Perform migrations
● Create resource controllers
● Other great tasks
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. // 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. 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. 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”