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.

22f21d5c22b930fd35dd98f25dedf6a4?s=128

Ben Edmunds

March 02, 2013
Tweet

More Decks by Ben Edmunds

Other Decks in Technology

Transcript

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

    March 2, 13
  2. 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
  3. 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
  4. 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
  5. What is it? PHP Web Framework Lightweight (half-stack) Open Source

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

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

    March 2, 13
  8. 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
  9. 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
  10. 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
  11. 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
  12. Let’s do some comparisons Saturday, March 2, 13

  13. Comparison MVC Design Sparks Lightweight Fast Excellent Docs Compatibility Technical

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

    Saturday, March 2, 13
  15. Comparison MVC Design Heavy Large Learning Curve Slow Decent Docs

    Modular Backed by ZEND Saturday, March 2, 13
  16. Comparison HMVC Design Lightweight Fast Excellent Docs Packages? Learned from

    other frameworks Saturday, March 2, 13
  17. MV[CR] Design Bundles Lightweight Fast Excellent Docs Threw out previous

    framework ideas and started over Comparison Saturday, March 2, 13
  18. OK, I’ll try it. How do I get started? Saturday,

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

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

    bundle:upgrade twilio Saturday, March 2, 13
  21. 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
  22. Structure application - config - controllers - models - views

    - routes.php - bundles.php - etc... laravel public - css - img - js - etc... Saturday, March 2, 13
  23. Create Route Create routes at: application/routes.php include() additional route definitions

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

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

    artisan test Saturday, March 2, 13
  26. 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
  27. 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
  28. What about an API? Routes to the rescue! Saturday, March

    2, 13
  29. 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
  30. 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
  31. Controllers OK, routes are cool and all, but what if

    I want to use controllers? Saturday, March 2, 13
  32. Controllers application/routes.php Route::controller(Controller::detect()); This will give you “CodeIgniter style” controller

    routing. Saturday, March 2, 13
  33. Shit just got real Saturday, March 2, 13

  34. Routes Saturday, March 2, 13

  35. Routes What methods are available: Route::get(); Route::post(); Route::put(); Route::delete(); Route::any();

    //all of the above Saturday, March 2, 13
  36. 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
  37. 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
  38. Route Filters Assign a filter to a route: Route::get('api/user', array('before'

    => 'auth', function() { ... })); Saturday, March 2, 13
  39. Route Filters Pattern matching adds a ton of flexibility: Route::filter('pattern:

    api/*', 'auth'); Saturday, March 2, 13
  40. Models Saturday, March 2, 13

  41. Models Fluent Query Builder Eloquent ORM Schema Builder Migrations Saturday,

    March 2, 13
  42. Fluent Query Builder Similar to CI DB Class Simple and

    Easy to Understand Used extensively by Eloquent ORM Saturday, March 2, 13
  43. Fluent Query Builder $user = DB::table('users')->where_email('example@gmail.com') ->first(); Query Row by

    Email $id = DB::table('users')->insert_get_id(array( 'email' => 'example@gmail.com' )); Insert & Get ID Saturday, March 2, 13
  44. Eloquent ORM Simple Expressive Efficient Saturday, March 2, 13

  45. Eloquent ORM class User extends Eloquent { } Define an

    Eloquent model class Post extends Eloquent { } Saturday, March 2, 13
  46. Eloquent ORM $user = User::where_email(‘ben.edmunds@gmail.com’)->first(); Simple query $user = Post::where('views',

    '>', 100)->get(); Slightly more advanced query Saturday, March 2, 13
  47. 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
  48. 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
  49. WTF!? ORMs Suck! Saturday, March 2, 13

  50. 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
  51. 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
  52. Views Saturday, March 2, 13

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

    view from a route Saturday, March 2, 13
  54. Views Route::get('/', function() { return View::make('home.index')->with('email', 'ben.edmunds@gmail.com'); Sending data to

    a view with magic methods Route::get('/', function() { $view = View::make('home.index'); $view->email = ‘ben.edmunds@gmail.com’; return $view; Sending data to a view Saturday, March 2, 13
  55. New Hotness Laravel 4 Saturday, March 2, 13

  56. Laravel 4 PSR-0 and PSR-1 Compliant Entirely Modular Composer Packages

    Focus on Testing Saturday, March 2, 13
  57. 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
  58. Laravel 4 Composer is the future of PHP Frameworks Imagine

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

    php composer.phar install Setup composer.json to define dependencies Saturday, March 2, 13
  60. 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
  61. Laravel 4 So how bad is the upgrade to L4

    Saturday, March 2, 13
  62. Laravel 4 Laravel 3 uses a shit-ton of statics Hard

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

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

    Keeps testing at the forefront Saturday, March 2, 13
  65. 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
  66. Laravel 4 $app['session'] = function() { return new MyCustomSessionLayer; }

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

    more: http://www.thenerdary.net/post/30859565484/laravel-4 Saturday, March 2, 13
  68. Laravel 4 Lastly, but most importantly, on the subject of

    unit testing Saturday, March 2, 13
  69. Laravel 4 Write Tests or Chris will cut you Saturday,

    March 2, 13
  70. 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
  71. 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
  72. Community http://github.com/laravel/laravel http://docs.laravel.com http://forums.laravel.com Saturday, March 2, 13

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

    Saturday, March 2, 13