Slide 1

Slide 1 text

An introduction to Laravel 4 by Dries Vints / @DriesVints An introduction to Laravel 4 By Dries Vints

Slide 2

Slide 2 text

An introduction to Laravel 4 by Dries Vints / @DriesVints About me • Web developer at Volta Web (3 years) • Volta Web is a small web agency in Antwerp • Weekly Laravel blog post series at driesvints.com

Slide 3

Slide 3 text

An introduction to Laravel 4 by Dries Vints / @DriesVints About Laravel • Created in 2011 by Taylor Otwell • For developer productivity and happiness • Clean and readable code • Rapidly growing and helpful community • Thoroughly documented and readable core

Slide 4

Slide 4 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Core concepts • PHP 5 >= 5.3.0 • Composer • RESTful • Testable

Slide 5

Slide 5 text

An introduction to Laravel 4 by Dries Vints / @DriesVints getcomposer.org

Slide 6

Slide 6 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Composer • packagist.org • Re-usable packages • Easily manage dependencies for each application • Very simple to use

Slide 7

Slide 7 text

An introduction to Laravel 4 by Dries Vints / @DriesVints { "require": { "laravel/framework": "4.0.*" }, ... } Laravel’s composer.json

Slide 8

Slide 8 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Some Laravel features • Easy routing • Authentication • Blade syntax • Migrations • Eloquent ORM

Slide 9

Slide 9 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Routing • Closures • Controller actions • RESTful controllers • Resourceful controllers

Slide 10

Slide 10 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // http://example.com/users Route::get('users', function() { // Get all users from DB. $users = User::all(); // Create the user list view. return View::make('users') ->with('users', $users); }); Route to closure

Slide 11

Slide 11 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // http://example.com/users Route::get('users', 'UsersController@index'); // http://example.com/users/1 Route::get('users/{id}', 'UsersController@show'); // http://example.com/users Route::post('users', 'UsersController@create'); Route to controller action

Slide 12

Slide 12 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::controller('users', 'UsersController'); class UsersController extends BaseController { // http://example.com/users/profile/1 function getProfile($id) { return 'A user with ID: ' . $id; } } Route to RESTful controller

Slide 13

Slide 13 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::resource('users', 'UsersController'); class UsersController extends BaseController { // http://example.com/users function index() { return 'All blog posts!'; } } Route to resource controller

Slide 14

Slide 14 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::group(array('prefix' => 'admin'), function() { // http://example.com/admin/users Route::get('users', function() { // }); }); Route groups

Slide 15

Slide 15 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::filter('old', function() { if (Input::get('age') < 200) { return Redirect::to('home'); } }); Route::get('user', array('before' => 'old', function() { return 'You are over 200 years old!'; })); Route filters

Slide 16

Slide 16 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Bind parameter to model. Route::model('user', 'User'); // http://example.com/profile/1 Route::get('profile/{user}', function(User $user) { // Do something with this user. }); Route Model Binding

Slide 17

Slide 17 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::bind('user', function($value, $route) { return User::where('name', $value)->first(); }); Custom Route binding

Slide 18

Slide 18 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::get('user', function() { return 'Everyone can access this route.'; }); Route::get('user', array('before' => 'auth', function() { return 'Only logged in users can access this route'; })); Securing routes

Slide 19

Slide 19 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::group(array('before' => 'auth'), function() { Route::get('users', 'UsersController@index'); Route::get('roles', 'RolesController@index'); }); Secure route groups

Slide 20

Slide 20 text

An introduction to Laravel 4 by Dries Vints / @DriesVints $credentials = array( 'email' => $email, 'password' => $password ); if (Auth::attempt($credentials)) { // The user's credentials are valid... } Authentication

Slide 21

Slide 21 text

An introduction to Laravel 4 by Dries Vints / @DriesVints @if (count($users)) @foreach ($users as $user)

{{ $user->name }}

{{ $user->bio }}

@endforeach @endif Views Blade syntax!

Slide 22

Slide 22 text

An introduction to Laravel 4 by Dries Vints / @DriesVints @section('sidebar') This is the master sidebar. @show
@yield('content')
Defining Blade layouts

Slide 23

Slide 23 text

An introduction to Laravel 4 by Dries Vints / @DriesVints @extends('layouts.master') @section('sidebar') @parent

This is appended to the master sidebar.

@stop @section('content')

This is my body content.

@stop Using Blade layouts

Slide 24

Slide 24 text

An introduction to Laravel 4 by Dries Vints / @DriesVints $environments = array( 'local' => array('MacBook-of-Dries', '*.dev'), 'staging' => array('dev.mywebsite.com'), 'production' => array('*.mywebsite.com'), ); Environments

Slide 25

Slide 25 text

An introduction to Laravel 4 by Dries Vints / @DriesVints app - config - database.php - cache.php - staging - database.php - production - database.php - cache.php Environments

Slide 26

Slide 26 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Artisan • CLI for Laravel • Driven by the Symfony/Console component • Used for common tasks like migrations • Provides helpers for code generating • Can be extended

Slide 27

Slide 27 text

An introduction to Laravel 4 by Dries Vints / @DriesVints # Starts up a PHP dev server (PHP 5.4). php artisan serve # Interact with your application. php artisan tinker # Generate a controller class. php artisan controller:make UsersController Some Artisan commands

Slide 28

Slide 28 text

An introduction to Laravel 4 by Dries Vints / @DriesVints # Put application in maintenance mode. php artisan down # Put application back live. php artisan up Maintenance mode

Slide 29

Slide 29 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Migrations • Version control for your database scheme • Unique by timestamp • Build and edit your database scheme • Reverse to previous iterations

Slide 30

Slide 30 text

An introduction to Laravel 4 by Dries Vints / @DriesVints class CreateUsersTable extends Migration { // Execute changes. public function up() {} // Reverse changes. public function down() {} } Migrations

Slide 31

Slide 31 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Migrate up. Schema::create('users', function($table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); // Migrate down. Schema::drop('users'); Schema builder

Slide 32

Slide 32 text

An introduction to Laravel 4 by Dries Vints / @DriesVints # Run migrations. php artisan migrate # Reverse previous migration. php artisan migrate:rollback # Fresh install. php artisan migrate:refresh Executing migrations

Slide 33

Slide 33 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Get multiple records. $users = DB::table('users')->get(); // Get a single record. $user = DB::table('users')->where('name', 'Dries') ->first(); // Get records as a key/value array. $users = DB::table('users')->lists('name', 'id'); Query builder

Slide 34

Slide 34 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Insert records. DB::table('users')->insert(array('name' => 'Paul')); // Update a record. DB::table('users')->where('id', 1) ->update(array('name' => 'Kerim')); // Delete records. DB::table('users')->where('id', '<=', 5)->delete(); Query builder

Slide 35

Slide 35 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Eloquent ORM • Based on Rail’s ActiveRecord • Query scoping • Makes defining relationships super easy • Model events • Much more...

Slide 36

Slide 36 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Just one simple rule. class User extends Eloquent {} // Get all users. $users = User::all(); // Find a specific user. $user = User::find(1); Basic Eloquent model

Slide 37

Slide 37 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Create a new user. $user = new User; $user->name = 'Jan'; $user->save(); // Or... $user = User::create(array('name' => 'Paul')); // Delete a record. User::destroy(1); CRUD with Eloquent

Slide 38

Slide 38 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Query builder. $user = DB::table('users')->where('name', 'Dries') ->first(); // Eloquent. $user = User::where('name', 'Dries')->first(); Query builder extended

Slide 39

Slide 39 text

An introduction to Laravel 4 by Dries Vints / @DriesVints class User extends Eloquent { public function scopePopular($query, $votes = 100) { return $query->where('votes', '>', $votes); } } $users = User::popular(50)->get(); Query scopes

Slide 40

Slide 40 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Eloquent relationships • One to one • One to many • Many to many • Polymorphic

Slide 41

Slide 41 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // The 'phones' table has a 'user_id' foreign key. class User extends Eloquent { public function phone() { return $this->hasOne('Phone'); } } // Get the user’s phone number. $phoneNumber = User::find(1)->phone->number; One to One

Slide 42

Slide 42 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // The 'phones' table has a 'user_id' column. class User extends Eloquent { public function phones() { return $this->hasMany('Phone'); } } // Itterate over the user’s phones. foreach ($user->phones as $phone) {} One to Many

Slide 43

Slide 43 text

An introduction to Laravel 4 by Dries Vints / @DriesVints class Phone extends Eloquent { public function user() { return $this->belongsTo('User'); } } // Get the user to which this phone belongs to. $user = Phone::find(1)->user; Inverse relation

Slide 44

Slide 44 text

An introduction to Laravel 4 by Dries Vints / @DriesVints class User extends Eloquent { public function roles() { return $this->belongsToMany('Role'); } } foreach ($user->roles as $role) {} foreach ($role->users as $user) {} Many to Many

Slide 45

Slide 45 text

An introduction to Laravel 4 by Dries Vints / @DriesVints class Photo extends Eloquent { public function imageable() { return $this->morphTo(); } } class User extends Eloquent { public function photos() { return $this->morphMany('Photo', 'imageable'); } } Polymorphic

Slide 46

Slide 46 text

An introduction to Laravel 4 by Dries Vints / @DriesVints users id - integer galleries id - integer photos id - integer imageable_id - integer imageable_type - string Polymorphic

Slide 47

Slide 47 text

An introduction to Laravel 4 by Dries Vints / @DriesVints // Retrieve a user’s or a gallery’s photos. foreach ($user->photos as $photo) {} foreach ($gallery->photos as $photo) {} // Retrieving the owner. $imageable = Photo::find(1)->imageable; // $imageable can be User or Gallery. Polymorphic

Slide 48

Slide 48 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Route::get('users', function() { return User::all(); }); JSON Great for building APIs!

Slide 49

Slide 49 text

An introduction to Laravel 4 by Dries Vints / @DriesVints [ { "id":1, "name":"Dries Vints", "bio":"Lorum ipsum.", "created_at":"2013-04-06 20:10:15", "updated_at":"2013-04-06 20:15:17" }, ... ] JSON

Slide 50

Slide 50 text

An introduction to Laravel 4 by Dries Vints / @DriesVints There’s more! • Events • Caching • Queues • Localization • Unit testing • Validation • Mailer • Pagination • Forms & HTML generator • Session handler • Logging • And tons of other stuff...

Slide 51

Slide 51 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Symfony & Laravel • Match made in heaven • Symfony provides solid components • Pre-set release cycle

Slide 52

Slide 52 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Symfony in Laravel 3 symfony/console & symfony/http-foundation

Slide 53

Slide 53 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Symfony in 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

Slide 54

Slide 54 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Other packages • classpreloader/classpreloader • doctrine/dbal • ircmaxell/password-compat • filp/whoops • monolog/monolog • nesbot/carbon • patchwork/utf8 • predis/predis • swiftmailer/swiftmailer

Slide 55

Slide 55 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Illuminate components 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

Slide 56

Slide 56 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Laravel’s release schedule Mapped to Symfony's schedule 4.0: May 2013 4.1: November 2013 4.2: May 2014 4.3: November 2014

Slide 57

Slide 57 text

An introduction to Laravel 4 by Dries Vints / @DriesVints How to learn Laravel 4? • http://four.laravel.com • http://net.tutsplus.com/ • http://laravel.io/ • http://daylerees.com • http://jasonlewis.me/laravel-tutorials • http://forums.laravel.io/viewtopic.php?id=859

Slide 58

Slide 58 text

An introduction to Laravel 4 by Dries Vints / @DriesVints The community • IRC #laravel on irc.freenode.net (+/- 350 people) • http://forums.laravel.io • #laravel on twitter

Slide 59

Slide 59 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Laracon EU • 30 - 31st August 2013 • At Bimhuis in Amsterdam • International speakers • They need sponsors! -> [email protected]

Slide 60

Slide 60 text

An introduction to Laravel 4 by Dries Vints / @DriesVints Thanks • @driesvints • github.com/driesvints • driesvints.com • https://joind.in/8571