Slide 1

Slide 1 text

Ben Edmunds @benedmunds http://benedmunds.com A Framework For Web Artisans Saturday, March 2, 13

Slide 2

Slide 2 text

Who Am I Ben Edmunds @benedmunds http://benedmunds.com Build and lead development teams >10 years of experience in the industry Specialize in PHP and Javascript Saturday, March 2, 13

Slide 3

Slide 3 text

Who Am I Ben Edmunds @benedmunds http://benedmunds.com Ion Auth http://github.com/benedmunds/CodeIgniter-Ion-Auth General http://github.com/benedmunds Open Source Contributions http://appstucco.com Saturday, March 2, 13

Slide 4

Slide 4 text

Who Am I Personal: From Phenix City, AL Married for 7 years Work for Kforce Government Solutions Co-Founder of AppStucco Tech Focus: PHP (CodeIgniter, Laravel) Javascript (Node.js, Express, Phonegap) Databases (MySQL, PostGreSQL, MongoDB) Ben Edmunds @benedmunds http://benedmunds.com Saturday, March 2, 13

Slide 5

Slide 5 text

What is it? PHP Web Framework Lightweight (half-stack) Open Source Saturday, March 2, 13

Slide 6

Slide 6 text

Who’s behind it? Dayle Rees @daylerees Taylor Otwell @taylorotwell Saturday, March 2, 13

Slide 7

Slide 7 text

I already use X. Why should I use Laravel? Saturday, March 2, 13

Slide 8

Slide 8 text

Why use it? Built on PHP 5.3 Sinatra-style Routing Bundles http://bundles.laravel.com/ Artisan CLI Tool Wave your PHP flag proudly that’s right, I use PHP sucka Expressive Syntax Eloquent ORM Well Documented Saturday, March 2, 13

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Why use it? Bundles http://bundles.laravel.com/ $ php artisan bundle:install twilio Artisan CLI Tool $ php artisan bundle:install twilio Well Documented http://laravel.com/docs Saturday, March 2, 13

Slide 11

Slide 11 text

Why use it? Sinatra-style Routing Eloquent ORM Route::get('/', function() { //do neat shit }); $user = new User; $user->id = 1; $user->save(); $user = User::find(1); Saturday, March 2, 13

Slide 12

Slide 12 text

Let’s do some comparisons Saturday, March 2, 13

Slide 13

Slide 13 text

Comparison MVC Design Sparks Lightweight Fast Excellent Docs Compatibility Technical Debt Saturday, March 2, 13

Slide 14

Slide 14 text

Comparison HMVC Design Lightweight-ish Fast Horrible Docs API Changes Frequently Saturday, March 2, 13

Slide 15

Slide 15 text

Comparison MVC Design Heavy Large Learning Curve Slow Decent Docs Modular Backed by ZEND Saturday, March 2, 13

Slide 16

Slide 16 text

Comparison HMVC Design Lightweight Fast Excellent Docs Packages? Learned from other frameworks Saturday, March 2, 13

Slide 17

Slide 17 text

MV[CR] Design Bundles Lightweight Fast Excellent Docs Threw out previous framework ideas and started over Comparison Saturday, March 2, 13

Slide 18

Slide 18 text

OK, I’ll try it. How do I get started? Saturday, March 2, 13

Slide 19

Slide 19 text

Download https://github.com/laravel/laravel $ git clone https://github.com/laravel/laravel.git . Saturday, March 2, 13

Slide 20

Slide 20 text

Bundles http://bundles.laravel.com/ $ php artisan bundle:install twilio $ php artisan bundle:upgrade twilio Saturday, March 2, 13

Slide 21

Slide 21 text

Structure MV[CR] Design Model View Controller || Route || Both Bundles Self contained modules Has it’s own config, models, routes, views, etc... Saturday, March 2, 13

Slide 22

Slide 22 text

Structure application - config - controllers - models - views - routes.php - bundles.php - etc... laravel public - css - img - js - etc... Saturday, March 2, 13

Slide 23

Slide 23 text

Create Route Create routes at: application/routes.php include() additional route definitions if needed Saturday, March 2, 13

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Unit Test Runs all tests located at: application/tests/*.test.php $ php artisan test Saturday, March 2, 13

Slide 26

Slide 26 text

Unit Test application/tests/routes.test.php Test that biotch! class TestRoutes extends PHPUnit_Framework_TestCase { public function testBase() { $response = Route::forward('GET', '/'); $this->assertEquals('Boom!', $response); } Saturday, March 2, 13

Slide 27

Slide 27 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 Saturday, March 2, 13

Slide 28

Slide 28 text

What about an API? Routes to the rescue! Saturday, March 2, 13

Slide 29

Slide 29 text

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

Slide 30

Slide 30 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 ); }); Saturday, March 2, 13

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Controllers application/routes.php Route::controller(Controller::detect()); This will give you “CodeIgniter style” controller routing. Saturday, March 2, 13

Slide 33

Slide 33 text

Shit just got real Saturday, March 2, 13

Slide 34

Slide 34 text

Routes Saturday, March 2, 13

Slide 35

Slide 35 text

Routes What methods are available: Route::get(); Route::post(); Route::put(); Route::delete(); Route::any(); //all of the above Saturday, March 2, 13

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Models Saturday, March 2, 13

Slide 41

Slide 41 text

Models Fluent Query Builder Eloquent ORM Schema Builder Migrations Saturday, March 2, 13

Slide 42

Slide 42 text

Fluent Query Builder Similar to CI DB Class Simple and Easy to Understand Used extensively by Eloquent ORM Saturday, March 2, 13

Slide 43

Slide 43 text

Fluent Query Builder $user = DB::table('users')->where_email('[email protected]') ->first(); Query Row by Email $id = DB::table('users')->insert_get_id(array( 'email' => '[email protected]' )); Insert & Get ID Saturday, March 2, 13

Slide 44

Slide 44 text

Eloquent ORM Simple Expressive Efficient Saturday, March 2, 13

Slide 45

Slide 45 text

Eloquent ORM class User extends Eloquent { } Define an Eloquent model class Post extends Eloquent { } Saturday, March 2, 13

Slide 46

Slide 46 text

Eloquent ORM $user = User::where_email(‘[email protected]’)->first(); Simple query $user = Post::where('views', '>', 100)->get(); Slightly more advanced query Saturday, March 2, 13

Slide 47

Slide 47 text

Eloquent ORM Relationships class Post extends Eloquent { public function user() { return $this->belongs_to('User'); } } Post::find(1)->user->email; Retrieve Post Creator Email Saturday, March 2, 13

Slide 48

Slide 48 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 } Saturday, March 2, 13

Slide 49

Slide 49 text

WTF!? ORMs Suck! Saturday, March 2, 13

Slide 50

Slide 50 text

Eloquent ORM 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, ...) Eager Loading FTW! Saturday, March 2, 13

Slide 51

Slide 51 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, ...) Saturday, March 2, 13

Slide 52

Slide 52 text

Views Saturday, March 2, 13

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

New Hotness Laravel 4 Saturday, March 2, 13

Slide 56

Slide 56 text

Laravel 4 PSR-0 and PSR-1 Compliant Entirely Modular Composer Packages Focus on Testing Saturday, March 2, 13

Slide 57

Slide 57 text

Laravel 4 PSR-0 and PSR-1 compliant Utilizes several Symfony modules Laravel modules can be easily used elsewhere using Composer Saturday, March 2, 13

Slide 58

Slide 58 text

Laravel 4 Composer is the future of PHP Frameworks Imagine a framework agnostic world Saturday, March 2, 13

Slide 59

Slide 59 text

Laravel 4 { "require": { "laravel/framework": "4.0.*@dev" } } Install php composer.phar install Setup composer.json to define dependencies Saturday, March 2, 13

Slide 60

Slide 60 text

Laravel 4 Updating with Composer php composer.phar update Updating your framework used to land you in copy/paste hell, not anymore Saturday, March 2, 13

Slide 61

Slide 61 text

Laravel 4 So how bad is the upgrade to L4 Saturday, March 2, 13

Slide 62

Slide 62 text

Laravel 4 Laravel 3 uses a shit-ton of statics Hard to test Beautiful API Saturday, March 2, 13

Slide 63

Slide 63 text

Laravel 4 Laravel 4 API is EXTREMELY similar Backwards compatible in most cases Easy to test Saturday, March 2, 13

Slide 64

Slide 64 text

Laravel 4 Uses Facades to expose a static style API Keeps testing at the forefront Saturday, March 2, 13

Slide 65

Slide 65 text

Laravel 4 $var = Session::get('foo'); Behind the scenes $app->resolve('session')->get('foo'); Getting a session variable (same as L3) Saturday, March 2, 13

Slide 66

Slide 66 text

Laravel 4 $app['session'] = function() { return new MyCustomSessionLayer; } Facades allow you to inject dependencies or override core functionality Saturday, March 2, 13

Slide 67

Slide 67 text

Laravel 4 Facade examples stolen from Kenny Myers To learn more: http://www.thenerdary.net/post/30859565484/laravel-4 Saturday, March 2, 13

Slide 68

Slide 68 text

Laravel 4 Lastly, but most importantly, on the subject of unit testing Saturday, March 2, 13

Slide 69

Slide 69 text

Laravel 4 Write Tests or Chris will cut you Saturday, March 2, 13

Slide 70

Slide 70 text

Laravel 4 Release Schedule Laravel 4.0 - May 2013 Laravel 4.1 - Nov 2013 Laravel 4.2 - May 2014 Laravel 4.3 - Nov 2014 Saturday, March 2, 13

Slide 71

Slide 71 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 shit Saturday, March 2, 13

Slide 72

Slide 72 text

Community http://github.com/laravel/laravel http://docs.laravel.com http://forums.laravel.com Saturday, March 2, 13

Slide 73

Slide 73 text

Ben Edmunds @benedmunds http://benedmunds.com A Framework For Web Artisans THANKS! Saturday, March 2, 13