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

SkiPHP 2014 - Intro to Laravel 4

SkiPHP 2014 - Intro to Laravel 4

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.

Ben Edmunds

January 15, 2014
Tweet

More Decks by Ben Edmunds

Other Decks in Programming

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 development !
  2. Who Am I Personal: From Alabama (hence the accent) Living

    in Portland, OR ! Tech Focus: PHP (CodeIgniter, Laravel, Slim) Javascript (Backbone, Angular, Node.js, Express, Phonegap) Databases (MySQL, PostGreSQL) Ben Edmunds @benedmunds http://benedmunds.com
  3. Why use it? Expressive Syntax Route::get('user/(:num)', function($id) { $user =

    User::find($id); ! return View::make(‘home') ->with('user', $user); });
  4. Why use it? Utilizes the latest PHP features <?php namespace

    Eloquent; ! use Laravel\Database as DB; ! abstract class Model {
  5. Why use it? Eloquent ORM $user = new User; $user->id

    = 1; $user->save(); ! $user = User::find(1);
  6. Comparison HMVC Design ! Lightweight ! ! Fast Excellent Docs

    ! Learned from other frameworks ! Community?
  7. Comparison MVC Design ! Heavy ! Slow ! Large Learning

    Curve Decent Docs ! Modular ! Backed by ZEND !
  8. Comparison MVC Design ! Heavy but has Silex ! Slow-ish

    ! Complex Good Docs ! Modular ! Backed by SensioLabs !
  9. MV[CR] Design ! Easy to get started ! Consistent/Stable Fast

    ! Excellent Docs ! Threw out previous framework ideas and started over ! Comparison
  10. Structure application - config - controllers - models - views

    - routes.php - etc... laravel public - css - img - js - etc...
  11. 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);
  12. 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
  13. 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 ); });
  14. Routes Register multiple verbs for a single route: ! Router::register(

    array('GET', 'POST'), $uri, function(){ //this will run for both get and post });
  15. Route Filters Run before or after a given route. !

    Register a filter: ! Route::filter('auth', function() { if (Auth::guest()) return Redirect::to('login'); });
  16. Route Filters Assign a filter to a route: ! Route::get('api/user',

    array( 'before' => 'auth', ), function() { ... }));
  17. Route Filters Pattern matching adds a ton of flexibility: !

    Route::filter('pattern: api/*', 'auth');
  18. Eloquent ORM Relationships class Post extends Eloquent { ! public

    function user() { return $this->belongsTo('User'); } ! } Post::find(1)->user->email; Retrieve Post Creator Email
  19. 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
  20. Eloquent ORM SELECT * FROM "posts" ! foreach result {

    SELECT * FROM "users" WHERE "id"=1 } 100 posts = 101 queries
  21. 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!
  22. Eloquent ORM SELECT * FROM "posts" ! SELECT * FROM

    "users" WHERE "id" IN (1, 2, 3, 4, 5, ...) 100 posts = 2 queries
  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 } 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, ...)
  24. Views Sending data to a view with magic methods Route::get('/',

    function(){ ! $view = View::make('home.index'); $view->email = ‘[email protected]’; ! return $view;
  25. Facades class HomeController extends BaseController { ! public function __construct(

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

    Illuminate\Session\Store $session, Illuminate\Http\Request $input ) { $this->session = $session; $this->input = $input; Controller Injection
  27. Laravel 4 ! Uses Facades to expose a static style

    API ! Keeps testing at the forefront
  28. Laravel 4 $app['session'] = function() { return new MyCustomSessionLayer; }

    ! Facades allow you to inject dependencies or override core functionality
  29. Updating Updating with Composer php composer.phar update ! Updating your

    framework used to land you in copy/paste hell, not anymore
  30. Releases Release Schedule Laravel 4.0 - May 2013 Laravel 4.1

    - Nov 2013 Laravel 4.2 - May 2014 Laravel 4.3 - Nov 2014
  31. 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