Introduction à Laravel 4
Concepts de base
•
•
•
•

PHP 5 >= 5.3.0	

Composer	

RESTful	

Testable

Introduction à Laravel 4
getcomposer.org
Introduction à Laravel 4
Composer
•
•
•
•

packagist.org	

Packages réutilisables	

Gérer facilement les dépendences des applications	

Très facile à utiliser

Introduction à Laravel 4
Installation
•
•

Composer	

Laravel phar

Introduction à Laravel 4
Installation
Composer

Laravel Phar

Introduction à Laravel 4
Introduction à Laravel 4
Le composer.json de Laravel

Introduction à Laravel 4
•
•
•
•
•

Quelques fonctionnalités de
Laravel
Routage facile d’utilisation	

Authentification ‘built-in’	

Syntaxe de template Blade	

Migrations	

Eloquent ORM

Introduction à Laravel 4
Routage / Routing
•
•
•
•

Closures	

Actions de controlleur	

Controlleurs RESTful	

Controlleurs ressourceful

Introduction à Laravel 4
Route avec closure

Introduction à Laravel 4

https://gist.github.com/nWidart/6334183
Route vers action controlleur

Introduction à Laravel 4

https://gist.github.com/nWidart/6334237
Route vers controlleur RESTful

Introduction à Laravel 4

https://gist.github.com/nWidart/6335300
Route vers controlleur resource

Introduction à Laravel 4

https://gist.github.com/nWidart/6335333
Route groups

Introduction à Laravel 4
Route filters

Introduction à Laravel 4
Route filters

Introduction à Laravel 4
Route filters

Introduction à Laravel 4
Sécuriser les routes

Introduction à Laravel 4
Secure route groups

Introduction à Laravel 4
Routes
Fonctions “avancées”
Paramètres optionels
Route::get('user/{name?}', function($name = 'Kai')	
{	
return $name;	
});	

Introduction à Laravel 4
Contraintes regex
Route::get('user/{name}', function($name)	
{	
//	
})	
->where('name', '[A-Za-z]+');	

Introduction à Laravel 4
Contraintes regex
Route::get('user/{name}', function($name)	
{	
//	
})	
->where('name', '[A-Za-z]+');	

Route::get('user/{id}', function($id)	
{	
//	
})	
->where('id', '[0-9]+');	

Introduction à Laravel 4
Contraintes regex
Route::get('user/{id}/{name}', function($id, $name)	
{	
//	
})	
->where(array('id' => '[0-9]+', 'name' => '[a-z]+'))	

!

Introduction à Laravel 4
Authentification
Authentification

Introduction à Laravel 4
Views
Views (blade)

Introduction à Laravel 4
Définir des layouts blade

Introduction à Laravel 4
Utiliser les blade layouts

Introduction à Laravel 4
Environements
Environements

Introduction à Laravel 4
Environements

Introduction à Laravel 4
Artisan
Artisan
•
•
•
•
•

CLI pour laravel	

Basé sur le composant Symfony/Console	

Utilisé pour des tâches régulières comme les migrations	

Offre des helpers pour génération de code	

Sait être étendu

Introduction à Laravel 4
Quelques commandes Artisan

Introduction à Laravel 4
Mode de maintenance

Introduction à Laravel 4
Migrations
Migrations
•
•
•
•

Gestions de versions pour la DB	

Unique par timestamp	

Construction & édition du layout DB	

Révenir vers des structures ultérieures

Introduction à Laravel 4
Migrations

Introduction à Laravel 4
Schema builder

Introduction à Laravel 4
Schema builder

Introduction à Laravel 4
Exécuter les migrations

Introduction à Laravel 4
Query builder
Query builder

Introduction à Laravel 4
Query builder

Introduction à Laravel 4
Eloquent ORM
Eloquent ORM
•
•
•
•
•

Basé sur ActiveRecord de Rails	

Query scoping	

Rends la définition de relations ultra facile	

Events de modèle	

Beaucoup plus...

Introduction à Laravel 4
Basic Eloquent model

Introduction à Laravel 4
CRUD avec Eloquent

Introduction à Laravel 4
Query Builder vs Eloquent

Introduction à Laravel 4
Query Scopes

Introduction à Laravel 4
Relations dans Eloquent
•
•
•
•

One to one	

One to many	

Many to many	

Polymorphic

Introduction à Laravel 4
One to One

Introduction à Laravel 4
One to One: inverse

Introduction à Laravel 4
One to One: inverse

Introduction à Laravel 4
One to Many
class Post extends Eloquent {	

!
public function comments()	
{	
return $this->hasMany('Comment');	
}	

!
}

$comments = Post::find(1)->comments;

Introduction à Laravel 4
One to Many: inverse
class Comment extends Eloquent {	

!
public function post()	
{	
return $this->belongsTo('Post');	
}	

!
}

$post = Comment::find(1)->post;

Introduction à Laravel 4
Many to Many
class User extends Eloquent {	

!
public function roles()	
{	
return $this->belongsToMany('Role');	
}	

!
}

$roles = User::find(1)->roles;

Introduction à Laravel 4
Polymorphic
class Staff extends Eloquent {	

class Photo extends Eloquent {	

!

!
public function imageable()	
{	
return $this->morphTo();	
}	

public function photos()	
{	
return $this->morphMany('Photo', 'imageable');	
}	

!

!

}

}	

class Order extends Eloquent {	

!
public function photos()	
{	
return $this->morphMany('Photo', 'imageable');	
}	

!
}
Introduction à Laravel 4
Polymorphic
staff	
id - integer	
name - string	

!
orders	
id - integer	
price - integer	

!
photos	
id - integer	
path - string	
imageable_id - integer	
imageable_type - string

Introduction à Laravel 4
Polymorphic: récupérer la relation

$staff = Staff::find(1);	

!
foreach ($staff->photos as $photo)	
{	
//	
}

Introduction à Laravel 4
Polymorphic: récupérer le owner

$photo = Photo::find(1);	

!
$imageable = $photo->imageable;

Introduction à Laravel 4
N+1 problem
N+1
Méthode traditionnelle

Introduction à Laravel 4
N+1
Méthode traditionnelle

100 posts = 101 requêtes SQL
SELECT * FROM “posts”	

foreach result {	

	

 SELECT * FROM “users” WHERE “id” = 1	

}

Introduction à Laravel 4
Solution
Eager Loading!
Solution
Eager Loading

Introduction à Laravel 4
Solution
Eager Loading

100 posts = 2 requêtes SQL
SELECT * FROM “posts”	

SELECT * FROM “users” WHERE “id” IN (1,2,3,4,…)

Introduction à Laravel 4
JSON
•

Super facile de créer des APIs

Introduction à Laravel 4
JSON

Route::get('users', function()	
{	
return User::all();	
});

Introduction à Laravel 4
JSON

Route::get('users', function()	
{	
return User::all();	
});

Introduction à Laravel 4
Validation
Validation
$validator = Validator::make(	
array('name' => 'Nicolas'),	
array('name' => 'required|min:5')	
);	

Introduction à Laravel 4
Validation
$validator = Validator::make(	
array('name' => 'Nicolas'),	
array('name' => 'required|min:5')	
);	

if ($validator->fails())	
{	
// Validation pas passé	
}	

Introduction à Laravel 4
Validation
// Récupérer les messages d’erreur	

!
$messages = $validator->messages();	

Introduction à Laravel 4
Validation
// Récupérer les messages d’erreur	

!
$messages = $validator->messages();	

// Récupérer le premier message d’erreur d’un champ	
echo $messages->first('name');	

Introduction à Laravel 4
Validation
// Récupérer les messages d’erreur	

!
$messages = $validator->messages();	

// Récupérer le premier message d’erreur d’un champ	
echo $messages->first('name');	

Introduction à Laravel 4

// Toutes les erreurs	
foreach ($messages->all() as $message)
Validation
// Récupérer les messages d’erreur	

!

// Toutes les erreurs	
foreach ($messages->all() as $message)

$messages = $validator->messages();	
// Toutes les erreurs d’un champ	
foreach ($messages->get('name') as $message)	
// Récupérer le premier message d’erreur d’un champ	
echo $messages->first('name');	

Introduction à Laravel 4
Validation
// Récupérer les messages d’erreur	

!

// Toutes les erreurs	
foreach ($messages->all() as $message)

$messages = $validator->messages();	
// Toutes les erreurs d’un champ	
foreach ($messages->get('name') as $message)	
// Récupérer le premier message d’erreur d’un champ	
echo $messages->first('name');	

Introduction à Laravel 4

// Check si erreurs	
if ($messages->has('name'))
Validation: example
Route::get('register', function()	
{	
return View::make('user.register');	
});	

!
Route::post('register', function()	
{	
$rules = array(...);	

!
$validator = Validator::make(Input::all(), $rules);	

!
if ($validator->fails())	
{	
return Redirect::to('register')->withErrors($validator);	
}	
});	

Introduction à Laravel 4
Ce n’est pas tout!
•
•
•
•
•
•

Evènements	

Caching	

Queues	

Localisation	

Unit Tests	

Mailer	


Introduction à Laravel 4

•
•
•
•
•

Pagination	

Formulaires & générateur HTML	

Gestion de sessions	

Logging	

Et des tonnes de choses en plus..
Symfony & Laravel
•
•
•

Symfony propose des composants stables et solides	

Release cycle prédéfini	

Couple parfait

Introduction à Laravel 4
Symfony dans Laravel 3
•
•

symfony/console	

symfony/http-foundation

Introduction à Laravel 4
Symfony dans Laravel 4
•
•
•
•
•
•

symfony/browser-kit	

symfony/console	

symfony/css-selector	

symfony/debug	

symfony/dom-crawler	

symfony/event-dispatcher	


Introduction à Laravel 4

•
•
•
•
•
•

symfony/finder	

symfony/http-foundation	

symfony/http-kernel	

symfony/process	

symfony/routing	

symfony/translation
Autres packages
•
•
•
•
•

classpreloader/classpreloader	

doctrine/dbal	

ircmaxell/password-compat	

filp/whoops	

monolog/monolog	


Introduction à Laravel 4

•
•
•
•

nesbot/carbon	

patchwork/utf8	

predis/predis	

swiftmailer/swiftmailer
Composants Illuminate
Auth, Cache, Config, Console, Container, Cookie, Database, Encryption,
Events, Exception, Filesystem, Foundation, Hashing, HTML, Http, Log,
Mail, Pagination, Queue, Redis, Routing, Session, Support, Translation,
Validation,View, Workbench

Introduction à Laravel 4
Laravel: planning
Ressemble au planning de sortie de Symfony	


•

4.0 - Mai 2013	


•

4.1 - Novembre 2013	


•

4.2 - Mai 2014	


•

4.3 - Novembre 2014

Introduction à Laravel 4
Mettre à jour Laravel
•
•

Dans terminal: ‘composer update’	

DONE

Introduction à Laravel 4
Ressources
•
•
•

Docs: http://laravel.com/docs/	

Laracasts: https://laracasts.com/	

Forum: http://forums.laravel.io/

Introduction à Laravel 4
Ressources: eBooks
•

Code Bright : https://leanpub.com/codebright

Introduction à Laravel 4
Ressources: eBooks
•

Laravel: From Apprentice To Artisan : https://leanpub.com/laravel

Introduction à Laravel 4
Ressources: eBooks
•

Laravel Testing Decoded : https://leanpub.com/laravel-testing-decoded

Introduction à Laravel 4
Merci.

Introduction à Laravel 4

Introduction à Laravel 4 @Dogstudio