introduction à laravel 4 @dogstudio
DESCRIPTION
A 2h introduction into the laravel 4 framework in french.TRANSCRIPT
Introduction à Laravel 4
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
InstallationComposer
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
https://gist.github.com/nWidart/6334183
Introduction à Laravel 4
Route vers action controlleur
https://gist.github.com/nWidart/6334237
Introduction à Laravel 4
Route vers controlleur RESTful
https://gist.github.com/nWidart/6335300
Introduction à Laravel 4
Route vers controlleur resource
https://gist.github.com/nWidart/6335333
Introduction à Laravel 4
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
RoutesFonctions “avancées”
Introduction à Laravel 4
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]+'))!
Authentification
Introduction à Laravel 4
Authentification
Views
Introduction à Laravel 4
Views (blade)
Introduction à Laravel 4
Définir des layouts blade
Introduction à Laravel 4
Utiliser les blade layouts
Environements
Introduction à Laravel 4
Environements
Introduction à Laravel 4
Environements
Artisan
Introduction à Laravel 4
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
Migrations
Introduction à Laravel 4
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
Query builder
Introduction à Laravel 4
Query builder
Introduction à Laravel 4
Query builder
Eloquent ORM
Introduction à Laravel 4
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
Polymorphicclass Photo extends Eloquent {! public function imageable() { return $this->morphTo(); }!}
class Staff extends Eloquent {! 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;
N+1 problem
Introduction à Laravel 4
N+1Méthode traditionnelle
Introduction à Laravel 4
N+1Méthode traditionnelle
SELECT * FROM “posts” foreach result { SELECT * FROM “users” WHERE “id” = 1 }
100 posts = 101 requêtes SQL
SolutionEager Loading!
Introduction à Laravel 4
SolutionEager Loading
Introduction à Laravel 4
SolutionEager Loading
SELECT * FROM “posts” SELECT * FROM “users” WHERE “id” IN (1,2,3,4,…)
100 posts = 2 requêtes SQL
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();});
Validation
Introduction à Laravel 4
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 champecho $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 champecho $messages->first('name');
// Toutes les erreursforeach ($messages->all() as $message)
Introduction à Laravel 4
Validation
// Récupérer les messages d’erreur!$messages = $validator->messages();
// Récupérer le premier message d’erreur d’un champecho $messages->first('name');
// Toutes les erreursforeach ($messages->all() as $message)
// Toutes les erreurs d’un champforeach ($messages->get('name') as $message)
Introduction à Laravel 4
Validation
// Récupérer les messages d’erreur!$messages = $validator->messages();
// Récupérer le premier message d’erreur d’un champecho $messages->first('name');
// Toutes les erreursforeach ($messages->all() as $message)
// Toutes les erreurs d’un champforeach ($messages->get('name') as $message)
// Check si erreursif ($messages->has('name'))
Introduction à Laravel 4
Validation: exampleRoute::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
• Pagination
• Formulaires & générateur HTML
• Gestion de sessions
• Logging
• Et des tonnes de choses en plus..
Introduction à Laravel 4
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
• symfony/finder
• symfony/http-foundation
• symfony/http-kernel
• symfony/process
• symfony/routing
• symfony/translation
Introduction à Laravel 4
Autres packages• classpreloader/classpreloader
• doctrine/dbal
• ircmaxell/password-compat
• filp/whoops
• monolog/monolog
• nesbot/carbon
• patchwork/utf8
• predis/predis
• swiftmailer/swiftmailer
Introduction à Laravel 4
Composants IlluminateAuth, 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: planningRessemble 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.