Share
Embed
An introduction to Laravel 4
A basic introduction talk to Laravel 4 which I gave on May 8, 2013.
Dries Vints
May 08, 2013
Transcript
60 slides
-
An introduction to Laravel 4 by Dries Vints / @DriesVints
An introduction to Laravel 4 By Dries Vints -
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 -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Core concepts • PHP 5 >= 5.3.0 • Composer • RESTful • Testable -
An introduction to Laravel 4 by Dries Vints / @DriesVints
getcomposer.org -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
{ "require": { "laravel/framework": "4.0.*" }, ... } Laravel’s composer.json -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Some Laravel features • Easy routing • Authentication • Blade syntax • Migrations • Eloquent ORM -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Routing • Closures • Controller actions • RESTful controllers • Resourceful controllers -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Route::bind('user', function($value, $route) { return User::where('name', $value)->first(); }); Custom Route binding -
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 -
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 -
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 -
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! -
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 -
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 -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
app - config - database.php - cache.php - staging - database.php - production - database.php - cache.php Environments -
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 -
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 -
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 -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
class CreateUsersTable extends Migration { // Execute changes. public function up() {} // Reverse changes. public function down() {} } Migrations -
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 -
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 -
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 -
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 -
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... -
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 -
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 -
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 -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Eloquent relationships • One to one • One to many • Many to many • Polymorphic -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Route::get('users', function() { return User::all(); }); JSON Great for building APIs! -
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 -
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... -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Symfony & Laravel • Match made in heaven • Symfony provides solid components • Pre-set release cycle -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Symfony in Laravel 3 symfony/console & symfony/http-foundation -
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 -
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 -
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 -
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 -
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 -
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 -
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] -
An introduction to Laravel 4 by Dries Vints / @DriesVints
Thanks • @driesvints • github.com/driesvints • driesvints.com • https://joind.in/8571