Slide 1

Slide 1 text

A Framework For Web Artisans

Slide 2

Slide 2 text

Ben Edmunds ! @benedmunds http://benedmunds.com Who is this guy?

Slide 3

Slide 3 text

Ben Edmunds ! Open Source Author PHP Town Hall Podcast CTO at Mindfulware Who is this guy?

Slide 4

Slide 4 text

What is it? PHP Web Framework Full-stack (what does that even mean anymore) Open Source

Slide 5

Slide 5 text

Who’s behind it? Dayle Rees @daylerees Taylor Otwell @taylorotwell

Slide 6

Slide 6 text

I already use X. Why should I use Laravel?

Slide 7

Slide 7 text

Why use it? Expressive Syntax Route::get('user/(:num)', function($id) { $user = User::find($id); ! return View::make(‘home') ->with('user', $user); });

Slide 8

Slide 8 text

Why use it? Utilizes the latest PHP features

Slide 9

Slide 9 text

Why use it? Facades = Quick Start Illuminate\Session\Store $session $session->put('key', 'value'); ! ! Session::put('key', 'value');

Slide 10

Slide 10 text

Why use it? Well Documented http://laravel.com/docs

Slide 11

Slide 11 text

Why use it? Artisan CLI Tool $ php artisan migrate

Slide 12

Slide 12 text

Why use it? Built on Composer http://packagist.com Entirely Modular PSR-0 and PSR-1 Compliant

Slide 13

Slide 13 text

Why use it? Good Test Coverage Focus on Testable Code

Slide 14

Slide 14 text

Why use it? Sinatra-style Routing Route::get(‘/users', function() { //list users });

Slide 15

Slide 15 text

Why use it? Eloquent ORM $user = new User; $user->id = 1; $user->save(); ! $user = User::find(1);

Slide 16

Slide 16 text

Why use it? Homestead Pre-made Vagrant Box Nginx MySQL/PostgreSQL Beanstalk Redis Memcached PHP !

Slide 17

Slide 17 text

Why use it? PAAS to quickly deploy Laravel apps to the “cloud” Digital Ocean Linode Amazon EC2 Rackspace !

Slide 18

Slide 18 text

Let’s do some comparisons

Slide 19

Slide 19 text

Comparison MVC Design ! Lightweight ! Fast ! Excellent Docs ! Compatibility ! Technical Debt !

Slide 20

Slide 20 text

Comparison HMVC Design ! Lightweight ! ! Fast Excellent Docs ! Learned from other frameworks ! Community?

Slide 21

Slide 21 text

Comparison MVC Design ! Heavy ! Slow ! Large Learning Curve Decent Docs ! Modular ! Backed by ZEND !

Slide 22

Slide 22 text

Comparison MVC Design ! Heavy but has Silex ! Slow-ish ! Complex Good Docs ! Modular ! Backed by SensioLabs !

Slide 23

Slide 23 text

MV[CR] Design ! Easy to get started ! Consistent/Stable Fast ! Excellent Docs ! Threw out previous framework ideas and started over ! Comparison

Slide 24

Slide 24 text

OK, I’ll try it. How do I get started?

Slide 25

Slide 25 text

Download Composer Based Install $ composer create-project laravel/laravel newProj --prefer-dist

Slide 26

Slide 26 text

Structure MV[CR] Design Model View Controller || Route || Both

Slide 27

Slide 27 text

Structure application - config - controllers - models - views - routes.php - etc... laravel public - css - img - js - etc...

Slide 28

Slide 28 text

Create Route Create routes at: application/routes.php ! include() additional route definitions if needed

Slide 29

Slide 29 text

Create Route application/routes.php Route::get('/', function(){ echo ‘Boom!’; });

Slide 30

Slide 30 text

Unit Test Runs all tests located at: application/tests/*.test.php $ php artisan test

Slide 31

Slide 31 text

Unit Test application/tests/routes.test.php Test it! Test it good! class TestRoutes extends PHPUnit_Framework_TestCase { ! public function testBase(){ $response = Route::forward('GET', '/'); $this->assertEquals('Boom!', $response);

Slide 32

Slide 32 text

Unit Test $ php artisan test Run it! PHPUnit 3.6.10 by Sebastian Bergmann. ! Configuration read from ...www/laravel/develop/phpunit.xml ! Time: 0 seconds, Memory: 6.25Mb ! OK (1 test, 1 assertion) Output

Slide 33

Slide 33 text

What about an API? Routes to the rescue!

Slide 34

Slide 34 text

API application/routes.php Route::get('api/user/(:num)', function($id){ return json_encode(array( ‘users‘ => User::find($id), ‘success‘ => true ); });

Slide 35

Slide 35 text

API application/routes.php Route::put('api/user', function(){ $user = User::create(array( 'email' => Input::get(‘email’) )); ! return json_encode(array( ‘user_id‘ => $user->id, ‘success‘ => true ); });

Slide 36

Slide 36 text

Controllers OK, routes are cool and all, but what if I want to use controllers?

Slide 37

Slide 37 text

Controllers application/routes.php Route::controller(Controller::detect()); This will give you standard FW style controller routing.

Slide 38

Slide 38 text

Shit just got real ! !

Slide 39

Slide 39 text

Routes ! !

Slide 40

Slide 40 text

Routes What methods are available: ! Route::get(); Route::post(); Route::put(); Route::delete(); ! Route::any(); //all of the above

Slide 41

Slide 41 text

Routes Register multiple verbs for a single route: ! Router::register( array('GET', 'POST'), $uri, function(){ //this will run for both get and post });

Slide 42

Slide 42 text

Route Filters Run before or after a given route. ! Register a filter: ! Route::filter('auth', function() { if (Auth::guest()) return Redirect::to('login'); });

Slide 43

Slide 43 text

Route Filters Assign a filter to a route: ! Route::get('api/user', array( 'before' => 'auth', ), function() { ... }));

Slide 44

Slide 44 text

Route Filters Pattern matching adds a ton of flexibility: ! Route::filter('pattern: api/*', 'auth');

Slide 45

Slide 45 text

Models ! !

Slide 46

Slide 46 text

Models Fluent Query Builder ! Eloquent ORM ! Schema Builder ! Migrations

Slide 47

Slide 47 text

Fluent Query Builder Simple and Easy to Understand ! Used extensively by Eloquent ORM

Slide 48

Slide 48 text

Fluent Query Builder $user = DB::table('users') ->whereEmail('[email protected]') ->first(); Query Row by Email

Slide 49

Slide 49 text

Fluent Query Builder $id = DB::table('users') ->insertGetId(array( 'email' => '[email protected]' )); Insert & Get ID

Slide 50

Slide 50 text

Eloquent ORM Simple ! Expressive ! Efficient

Slide 51

Slide 51 text

Eloquent ORM class User extends Eloquent { ! } Define an Eloquent model

Slide 52

Slide 52 text

Eloquent ORM $user = User::whereEmail(‘[email protected]’) ->first(); Simple query

Slide 53

Slide 53 text

Eloquent ORM $user = Post::where('views', '>', 100) ->get(); Slightly more advanced query

Slide 54

Slide 54 text

Eloquent ORM Relationships class Post extends Eloquent { ! public function user() { return $this->belongsTo('User'); } ! } Post::find(1)->user->email; Retrieve Post Creator Email

Slide 55

Slide 55 text

Eloquent ORM Normal Usage $posts = Post::all(); foreach ($posts as $post) echo $post->user->email;

Slide 56

Slide 56 text

Eloquent ORM Normal Usage $posts = Post::all(); foreach ($posts as $post) echo $post->user->email; SELECT * FROM "posts" ! foreach result { SELECT * FROM "users" WHERE "id" = 1 } 100 posts = 101 queries

Slide 57

Slide 57 text

Eloquent ORM SELECT * FROM "posts" ! foreach result { SELECT * FROM "users" WHERE "id"=1 } 100 posts = 101 queries

Slide 58

Slide 58 text

WTF!? ORMs Suck! ! !

Slide 59

Slide 59 text

Eloquent ORM $posts = Post::with(‘user’)->get(); foreach ($posts as $post) echo $post->user->email; Eager Loading FTW!

Slide 60

Slide 60 text

Eloquent ORM $posts = Post::with(‘user’)->get(); foreach ($posts as $post) echo $post->user->email; 100 posts = 2 queries ! SELECT * FROM "posts" ! SELECT * FROM "users" WHERE "id" IN (1, 2, 3, 4, 5, ...) Eager Loading FTW!

Slide 61

Slide 61 text

Eloquent ORM SELECT * FROM "posts" ! SELECT * FROM "users" WHERE "id" IN (1, 2, 3, 4, 5, ...) 100 posts = 2 queries

Slide 62

Slide 62 text

Eloquent ORM Normal Usage $posts = Post::all(); foreach ($posts as $post) { echo $post->user->email; } 100 posts = 101 queries ! SELECT * FROM "posts" ! foreach result { SELECT * FROM "users" WHERE "id" = 1 } Eager Loading $posts = Post::with(‘user’)->get(); foreach ($posts as $post) { echo $post->user->email; } 100 posts = 2 queries ! SELECT * FROM "posts" ! SELECT * FROM "users" WHERE "id" IN (1, 2, 3, 4, 5, ...)

Slide 63

Slide 63 text

Views ! !

Slide 64

Slide 64 text

Views Route::get('/', function(){ ! return View::make('home.index'); //views/home/index.php ! }); Returning a view from a route

Slide 65

Slide 65 text

Views Route::get('/', function(){ ! return View::make(‘home.index') ->with('email', '[email protected]'); Sending data to a view

Slide 66

Slide 66 text

Views Sending data to a view with magic methods Route::get('/', function(){ ! $view = View::make('home.index'); $view->email = ‘[email protected]’; ! return $view;

Slide 67

Slide 67 text

Facades ! !

Slide 68

Slide 68 text

Facades $var = Session::get('foo'); Behind the scenes $app->resolve('session')->get('foo'); Getting a session variable

Slide 69

Slide 69 text

Facades Controller Injection to the rescue! Managing Class Responsibilities?

Slide 70

Slide 70 text

Facades class HomeController extends BaseController { ! public function __construct( Illuminate\Session\Store $session ) { $this->session = $session; } Controller Injection

Slide 71

Slide 71 text

Facades class HomeController extends BaseController { ! public function __construct( Illuminate\Session\Store $session, Illuminate\Http\Request $input ) { $this->session = $session; $this->input = $input; Controller Injection

Slide 72

Slide 72 text

Drama ! Da da DOM

Slide 73

Slide 73 text

Facades $app['session'] = function() { return new MyCustomSessionLayer; } ! Facades allow you to inject dependencies or override core functionality

Slide 74

Slide 74 text

Updating ! Keeping Up With FW Changes

Slide 75

Slide 75 text

Updating Updating with Composer php composer.phar update ! Updating your framework used to land you in copy/paste hell, not anymore

Slide 76

Slide 76 text

Releases Release Schedule Laravel 4.0 - May 2013 Laravel 4.1 - Nov 2013 Laravel 4.2 - May 2014 Laravel 4.3 - Nov 2014

Slide 77

Slide 77 text

That’s a Wrap ! ! Utilizes the best PHP has to offer Lightweight and crazy fast Well designed API with great docs ! Go make cool things and have fun

Slide 78

Slide 78 text

Community http://github.com/laravel/laravel http://docs.laravel.com http://forums.laravel.com

Slide 79

Slide 79 text

Resources Dayle Rees - Code Bright Laravel: From Apprentice to Artisan LaraCasts

Slide 80

Slide 80 text

THANKS! Ben Edmunds @benedmunds http://benedmunds.com