Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An introduction to Laravel 4

An introduction to Laravel 4

A basic introduction talk to Laravel 4 which I gave on May 8, 2013.

Dries Vints

May 08, 2013
Tweet

More Decks by Dries Vints

Other Decks in Programming

Transcript

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

    An introduction to Laravel 4 By Dries Vints
  2. 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
  3. 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
  4. An introduction to Laravel 4 by Dries Vints / @DriesVints

    Core concepts • PHP 5 >= 5.3.0 • Composer • RESTful • Testable
  5. 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
  6. An introduction to Laravel 4 by Dries Vints / @DriesVints

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

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

    Routing • Closures • Controller actions • RESTful controllers • Resourceful controllers
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. An introduction to Laravel 4 by Dries Vints / @DriesVints

    Route::bind('user', function($value, $route) { return User::where('name', $value)->first(); }); Custom Route binding
  17. 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
  18. 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
  19. 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
  20. An introduction to Laravel 4 by Dries Vints / @DriesVints

    @if (count($users)) @foreach ($users as $user) <h2>{{ $user->name }}</h2> <p>{{ $user->bio }}</p> @endforeach @endif Views Blade syntax!
  21. An introduction to Laravel 4 by Dries Vints / @DriesVints

    <html> <body> @section('sidebar') This is the master sidebar. @show <div class="container"> @yield('content') </div> </body> </html> Defining Blade layouts
  22. An introduction to Laravel 4 by Dries Vints / @DriesVints

    @extends('layouts.master') @section('sidebar') @parent <p>This is appended to the master sidebar.</p> @stop @section('content') <p>This is my body content.</p> @stop Using Blade layouts
  23. 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
  24. An introduction to Laravel 4 by Dries Vints / @DriesVints

    app - config - database.php - cache.php - staging - database.php - production - database.php - cache.php Environments
  25. 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
  26. 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
  27. 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
  28. 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
  29. An introduction to Laravel 4 by Dries Vints / @DriesVints

    class CreateUsersTable extends Migration { // Execute changes. public function up() {} // Reverse changes. public function down() {} } Migrations
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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...
  35. 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
  36. 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
  37. 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
  38. 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
  39. An introduction to Laravel 4 by Dries Vints / @DriesVints

    Eloquent relationships • One to one • One to many • Many to many • Polymorphic
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. An introduction to Laravel 4 by Dries Vints / @DriesVints

    Route::get('users', function() { return User::all(); }); JSON Great for building APIs!
  48. 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
  49. 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...
  50. An introduction to Laravel 4 by Dries Vints / @DriesVints

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

    Symfony in Laravel 3 symfony/console & symfony/http-foundation
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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]
  59. An introduction to Laravel 4 by Dries Vints / @DriesVints

    Thanks • @driesvints • github.com/driesvints • driesvints.com • https://joind.in/8571