Slide 1

Slide 1 text

Ben Edmunds @benedmunds http://benedmunds.com A Framework For Web Artisans

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 development

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

Slide 4

Slide 4 text

Who Am I Personal: From Phenix City, AL Married for 6 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

Slide 5

Slide 5 text

What is it? PHP Web Framework Lightweight (half-stack) Open Source

Slide 6

Slide 6 text

Who’s behind it? Eric Barnes @ericlbarnes Taylor Otwell @taylorotwell Ian Landsman @ianlandsman

Slide 7

Slide 7 text

I already use X. Why should I use Laravel?

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

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

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);

Slide 12

Slide 12 text

Let’s do some comparisons

Slide 13

Slide 13 text

Comparison MVC Design Sparks Lightweight Fast Excellent Docs Compatibility Technical Debt

Slide 14

Slide 14 text

Comparison HMVC Design Lightweight-ish Fast Horrible Docs API Changes Frequently

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

MV[CR] Design Bundles Lightweight Fast Excellent Docs Threw out previous framework ideas and started over Comparison

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Download https://github.com/laravel/laravel $ git clone https://github.com/laravel/laravel.git .

Slide 20

Slide 20 text

Bundles http://bundles.laravel.com/ $ php artisan bundle:install twilio $ php artisan bundle:upgrade twilio

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...

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

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); }

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

Slide 28

Slide 28 text

What about an API? Routes to the rescue!

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 ); });

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 ); });

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Shit just got real

Slide 34

Slide 34 text

Routes

Slide 35

Slide 35 text

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

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 });

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'); });

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Models

Slide 41

Slide 41 text

Models Fluent Query Builder Eloquent ORM Schema Builder Migrations

Slide 42

Slide 42 text

Fluent Query Builder Similar to CI DB Class Simple and Easy to Understand Used extensively by Eloquent ORM

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

Slide 44

Slide 44 text

Eloquent ORM Simple Expressive Efficient

Slide 45

Slide 45 text

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

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

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

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 }

Slide 49

Slide 49 text

WTF!? ORMs Suck!

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!

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, ...)

Slide 52

Slide 52 text

Views

Slide 53

Slide 53 text

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

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

Slide 55

Slide 55 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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Ben Edmunds @benedmunds http://benedmunds.com A Framework For Web Artisans THANKS!