introduction à laravel 4 @dogstudio

95
Introduction à Laravel 4

Upload: nicolas-widart

Post on 07-Dec-2014

1.198 views

Category:

Technology


3 download

DESCRIPTION

A 2h introduction into the laravel 4 framework in french.

TRANSCRIPT

Page 1: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Page 2: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Concepts de base• PHP 5 >= 5.3.0

• Composer

• RESTful

• Testable

Page 3: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

getcomposer.org

Page 4: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Composer• packagist.org

• Packages réutilisables

• Gérer facilement les dépendences des applications

• Très facile à utiliser

Page 5: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Installation• Composer

• Laravel phar

Page 6: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

InstallationComposer

Laravel Phar

Page 7: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Page 8: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Le composer.json de Laravel

Page 9: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Quelques fonctionnalités de Laravel

• Routage facile d’utilisation

• Authentification ‘built-in’

• Syntaxe de template Blade

• Migrations

• Eloquent ORM

Page 10: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Routage / Routing• Closures

• Actions de controlleur

• Controlleurs RESTful

• Controlleurs ressourceful

Page 11: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route avec closure

https://gist.github.com/nWidart/6334183

Page 12: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route vers action controlleur

https://gist.github.com/nWidart/6334237

Page 13: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route vers controlleur RESTful

https://gist.github.com/nWidart/6335300

Page 14: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route vers controlleur resource

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

Page 15: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route groups

Page 16: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route filters

Page 17: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route filters

Page 18: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Route filters

Page 19: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Sécuriser les routes

Page 20: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Secure route groups

Page 21: Introduction à Laravel 4 @Dogstudio

RoutesFonctions “avancées”

Page 22: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Paramètres optionels

Route::get('user/{name?}', function($name = 'Kai'){ return $name;});

Page 23: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Contraintes regex

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

Page 24: Introduction à Laravel 4 @Dogstudio

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]+');

Page 25: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Contraintes regex

Route::get('user/{id}/{name}', function($id, $name){ //})->where(array('id' => '[0-9]+', 'name' => '[a-z]+'))!

Page 26: Introduction à Laravel 4 @Dogstudio

Authentification

Page 27: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Authentification

Page 28: Introduction à Laravel 4 @Dogstudio

Views

Page 29: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Views (blade)

Page 30: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Définir des layouts blade

Page 31: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Utiliser les blade layouts

Page 32: Introduction à Laravel 4 @Dogstudio

Environements

Page 33: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Environements

Page 34: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Environements

Page 35: Introduction à Laravel 4 @Dogstudio

Artisan

Page 36: Introduction à Laravel 4 @Dogstudio

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

Page 37: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Quelques commandes Artisan

Page 38: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Mode de maintenance

Page 39: Introduction à Laravel 4 @Dogstudio

Migrations

Page 40: Introduction à Laravel 4 @Dogstudio

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

Page 41: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Migrations

Page 42: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Schema builder

Page 43: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Schema builder

Page 44: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Exécuter les migrations

Page 45: Introduction à Laravel 4 @Dogstudio

Query builder

Page 46: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Query builder

Page 47: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Query builder

Page 48: Introduction à Laravel 4 @Dogstudio

Eloquent ORM

Page 49: Introduction à Laravel 4 @Dogstudio

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

Page 50: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Basic Eloquent model

Page 51: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

CRUD avec Eloquent

Page 52: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Query Builder vs Eloquent

Page 53: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Query Scopes

Page 54: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Relations dans Eloquent• One to one

• One to many

• Many to many

• Polymorphic

Page 55: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

One to One

Page 56: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

One to One: inverse

Page 57: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

One to One: inverse

Page 58: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

One to Many

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

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

Page 59: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

One to Many: inverse

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

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

Page 60: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Many to Many

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

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

Page 61: Introduction à Laravel 4 @Dogstudio

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'); }!}

Page 62: Introduction à Laravel 4 @Dogstudio

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

Page 63: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Polymorphic: récupérer la relation

$staff = Staff::find(1);!foreach ($staff->photos as $photo){ //}

Page 64: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Polymorphic: récupérer le owner

$photo = Photo::find(1);!$imageable = $photo->imageable;

Page 65: Introduction à Laravel 4 @Dogstudio

N+1 problem

Page 66: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

N+1Méthode traditionnelle

Page 67: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

N+1Méthode traditionnelle

SELECT * FROM “posts” foreach result { SELECT * FROM “users” WHERE “id” = 1 }

100 posts = 101 requêtes SQL

Page 68: Introduction à Laravel 4 @Dogstudio

SolutionEager Loading!

Page 69: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

SolutionEager Loading

Page 70: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

SolutionEager Loading

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

100 posts = 2 requêtes SQL

Page 71: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

JSON• Super facile de créer des APIs

Page 72: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

JSON

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

Page 73: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

JSON

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

Page 74: Introduction à Laravel 4 @Dogstudio

Validation

Page 75: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Validation

$validator = Validator::make( array('name' => 'Nicolas'), array('name' => 'required|min:5'));

Page 76: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Validation

$validator = Validator::make( array('name' => 'Nicolas'), array('name' => 'required|min:5'));

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

Page 77: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Validation

// Récupérer les messages d’erreur!$messages = $validator->messages();

Page 78: Introduction à Laravel 4 @Dogstudio

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');

Page 79: Introduction à Laravel 4 @Dogstudio

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)

Page 80: Introduction à Laravel 4 @Dogstudio

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)

Page 81: Introduction à Laravel 4 @Dogstudio

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'))

Page 82: Introduction à Laravel 4 @Dogstudio

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); }});

Page 83: Introduction à Laravel 4 @Dogstudio

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

Page 84: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Symfony & Laravel• Symfony propose des composants stables et solides

• Release cycle prédéfini

• Couple parfait

Page 85: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Symfony dans Laravel 3• symfony/console

• symfony/http-foundation

Page 86: Introduction à Laravel 4 @Dogstudio

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

Page 87: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Autres packages• classpreloader/classpreloader

• doctrine/dbal

• ircmaxell/password-compat

• filp/whoops

• monolog/monolog

• nesbot/carbon

• patchwork/utf8

• predis/predis

• swiftmailer/swiftmailer

Page 88: Introduction à Laravel 4 @Dogstudio

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

Page 89: Introduction à Laravel 4 @Dogstudio

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

Page 90: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Mettre à jour Laravel• Dans terminal: ‘composer update’

• DONE

Page 91: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

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

• Laracasts: https://laracasts.com/

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

Page 92: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Ressources: eBooks• Code Bright : https://leanpub.com/codebright

Page 93: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

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

Page 94: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

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

Page 95: Introduction à Laravel 4 @Dogstudio

Introduction à Laravel 4

Merci.