Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MidwestPHP - Intro to Laravel

MidwestPHP - Intro to Laravel

Are you ashamed to admit you're a PHP developer? Have you been using the same old, boring PHP framework for years? Tired of your PHP applications turning into enormous beasts? Maybe you've heard about Laravel but haven't made the effort to dive into it? In this presentation, we'll walk through what makes Laravel an elegant, fun, and exciting framework to make PHP applications that you'll be proud of. - See more at: http://midwestphp.com/sessions/Intro_to_Laravel#sthash.6xhUd5Qv.dpuf

Intro to Laravel presented by Ben Edmunds at MidwestPHP on March 2nd, 2013.

Ben Edmunds

March 02, 2013
Tweet

More Decks by Ben Edmunds

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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); }); <?php namespace Eloquent; use Laravel\Database as DB; abstract class Model { Saturday, March 2, 13
  6. 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
  7. 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
  8. Comparison MVC Design Heavy Large Learning Curve Slow Decent Docs

    Modular Backed by ZEND Saturday, March 2, 13
  9. MV[CR] Design Bundles Lightweight Fast Excellent Docs Threw out previous

    framework ideas and started over Comparison Saturday, March 2, 13
  10. 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
  11. Structure application - config - controllers - models - views

    - routes.php - bundles.php - etc... laravel public - css - img - js - etc... Saturday, March 2, 13
  12. 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
  13. 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
  14. 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
  15. Controllers OK, routes are cool and all, but what if

    I want to use controllers? Saturday, March 2, 13
  16. 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
  17. 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
  18. Route Filters Assign a filter to a route: Route::get('api/user', array('before'

    => 'auth', function() { ... })); Saturday, March 2, 13
  19. Fluent Query Builder Similar to CI DB Class Simple and

    Easy to Understand Used extensively by Eloquent ORM Saturday, March 2, 13
  20. 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
  21. Eloquent ORM class User extends Eloquent { } Define an

    Eloquent model class Post extends Eloquent { } Saturday, March 2, 13
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. Laravel 4 Composer is the future of PHP Frameworks Imagine

    a framework agnostic world Saturday, March 2, 13
  29. Laravel 4 { "require": { "laravel/framework": "4.0.*@dev" } } Install

    php composer.phar install Setup composer.json to define dependencies Saturday, March 2, 13
  30. 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
  31. Laravel 4 Laravel 3 uses a shit-ton of statics Hard

    to test Beautiful API Saturday, March 2, 13
  32. Laravel 4 Laravel 4 API is EXTREMELY similar Backwards compatible

    in most cases Easy to test Saturday, March 2, 13
  33. Laravel 4 Uses Facades to expose a static style API

    Keeps testing at the forefront Saturday, March 2, 13
  34. Laravel 4 $app['session'] = function() { return new MyCustomSessionLayer; }

    Facades allow you to inject dependencies or override core functionality Saturday, March 2, 13
  35. Laravel 4 Facade examples stolen from Kenny Myers To learn

    more: http://www.thenerdary.net/post/30859565484/laravel-4 Saturday, March 2, 13
  36. 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
  37. 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