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.

E8321183acdf47a9ce838afd13a964b5?s=128

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

    getcomposer.org
  6. 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
  7. An introduction to Laravel 4 by Dries Vints / @DriesVints

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

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

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

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

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

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

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

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

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

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

    Laracon EU • 30 - 31st August 2013 • At Bimhuis in Amsterdam • International speakers • They need sponsors! -> contact@laracon.eu
  60. An introduction to Laravel 4 by Dries Vints / @DriesVints

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