$30 off During Our Annual Pro Sale. View Details »

Per Your Request

Kenny Meyers
February 22, 2013

Per Your Request

How does Laravel 4 work? What precisely happens when a user loads index.php? Per Your Request walks through Laravel's entire request cycle, and shows you what happens when.

Kenny Meyers

February 22, 2013
Tweet

Other Decks in Programming

Transcript

  1. Pe You Request @kennymeyers thenerdary.net

  2. Request Cycle. Everything that happens (classes loaded, patterns invoked, etc.)

  3. 4 Phase of Laravel Loading

  4. It a begin with index.ph /public/index.php

  5. Phase 1: Autoloadin’

  6. Autoloadin‘ Application Bootstrapped Run 1. LARAVEL_START constant is set 2.

    index.php requires /bootstrap/autoload.php 3. bootstrap.php requires composer’s /vendor/ autoload file 4. Register Laravel’s custom ClassLoader 5. Register the workbench’s autoload files Phase I Commence
  7. LARAVEL_START global is set Autoloadin‘ Application Bootstrapped Run Grrrrreat! for

    benchmarking your code
  8. index.php requires /bootstrap/autoload.php /public/index.php Autoloadin‘ Application Bootstrapped Run

  9. autoload.php requires Composer’s autoload /bootstrap/autoload.php Autoloadin‘ Application Bootstrapped Run

  10. Derp, derp composer derp. Derp derp PyroCMS Phil’s got your

    back on composer
  11. All classes are ultimately loaded via The worst named function

    since [Java or C joke] Autoloadin‘ Application Bootstrapped Run
  12. spl_autoload_registe i a queue

  13. Register Laravel’s custom autoloader /bootstrap/autoload.php Autoloadin‘ Application Bootstrapped Run

  14. ClassLoader::register() Illuminate\Support\ClassLoader ClassLoader::load() Autoloadin‘ Application Bootstrapped Run

  15. Workbench autoloading /bootstrap/autoload.php Autoloadin‘ Application Bootstrapped Run

  16. Illuminate\Workbench\Starter Starter::start($workbench) Autoloadin‘ Application Bootstrapped Run

  17. Finder Class Symfony\Component\Finder Implements Countable, IteratorAggregate Traverses a given directory

    and returns an array of objects with some lovely file metadata Autoloadin‘ Application Bootstrapped Run
  18. Illuminate\Workbench\Starter Starter::start($workbench) Autoloadin‘ Application Bootstrapped Run Get all autoload.php files

    in the workbench folder
  19. Illuminate\Workbench\Starter Autoloadin‘ Application Bootstrapped Run Starter::start($workbench)

  20. Filesystem Class wrapper for common operations Illuminate\Workbench\Starter Autoloadin‘ Application Bootstrapped

    Run Filesystem::requireOnce()
  21. Phase I Complete Autoloadin‘ Application Bootstrapped Run 1. LARAVEL_START constant

    is set 2. index.php requires /bootstrap/autoload.php 3. bootstrap requires composer’s /vendor/autoload file 4. Register Laravel’s custom ClassLoader 5. Register the workbench’s autoload files
  22. Phase 2: A lication

  23. /public/index.php We RETURN to index.php Autoloadin‘ Application Bootstrapped Run

  24. Autoloadin‘ Application Bootstrapped Run 1. Require /bootstrap/start.php 2.Create a new

    instance of Illuminate\Foundation \Application 3. This creates the request object and registers 3 ServiceProviders 4. Detect and set the environment 5. Bind our application filepaths to the Application object 6. Require the Foundation start.php file via Application::getBootstrapFile() Phase 2 Commence
  25. /public/index.php Laravel requires /bootstrap/start.php Autoloadin‘ Application Bootstrapped Run

  26. /bootstrap/start.php BEHOLD! INSTANTIATION! Autoloadin‘ Application Bootstrapped Run

  27. THE LARAVEL APPLICATION HAS BEGUN! Autoloadin‘ Application Bootstrapped Run

  28. Illuminate\Foundation\Application Container Autoloadin‘ Application Bootstrapped Run

  29. Illuminate\Foundation\Container Four methods to know Autoloadin‘ Application Bootstrapped Run Container::share()

    Container::instance() Container::bind() Container::make()
  30. Illuminate\Foundation\Container Container::share() Autoloadin‘ Application Bootstrapped Run

  31. Illuminate\Foundation\Container Container::instance() Autoloadin‘ Application Bootstrapped Run

  32. Illuminate\Foundation\Container Container::bind() Autoloadin‘ Application Bootstrapped Run

  33. Illuminate\Foundation\Container Container::make() Autoloadin‘ Application Bootstrapped Run

  34. \Illuminate\Container\Container Implements ArrayAccess Autoloadin‘ Application Bootstrapped Run

  35. ArrayAccess lets us do this: Autoloadin‘ Application Bootstrapped Run Which

    calls Application::offsetSet()
  36. __construct() Autoloadin‘ Application Bootstrapped Run Illuminate\Foundation\Application

  37. Illuminate\Foundation\Application Application::register() Autoloadin‘ Application Bootstrapped Run

  38. Illuminate\Exception\ExceptionServiceProvider ExceptionServiceProvider::register() Autoloadin‘ Application Bootstrapped Run

  39. /bootstrap/start.php Laravel sets the $env variable Autoloadin‘ Application Bootstrapped Run

  40. /bootstrap/start.php Binds install paths Binds install paths Autoloadin‘ Application Bootstrapped

    Run
  41. /bootstrap/start.php Loads the Iluminate/Foundation/start.php file via Application::getBootstrapFile() Autoloadin‘ Application Bootstrapped

    Run
  42. Keeps things clean with composer Autoloadin‘ Application Bootstrapped Run

  43. Autoloadin‘ Application Bootstrapped Run Phase 2 Complete 1. Require /bootstrap/start.php

    2.Create a new instance of Illuminate\Foundation \Application 3. This creates the request object and registers 3 ServiceProviders 4. Detect and set the environment 5. Bind our application filepaths to the Application object 6. Require the Foundation start.php file via Application::getBootstrapFile()
  44. Phase 3: B tstra ed

  45. 1. Set the $env var if we’re running tests 2.

    Setup the Facade 3. Load the config files 4. Set the default timezone 5. Register the alias loader 6. Allow for PUT and DELETE requests with forms 7. Create the provider repository 8. Boot ServiceProviders with Application::boot() 9. Require the /app/start/global.php file 10. Require the /app/start/$env.php file 11. Require the /app/routes.php file Phase 3 Commence Autoloadin‘ Application Bootstrapped Run
  46. Testin’ check. Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  47. First, let’s turn off error handling and report all errors

    Autoloadin‘ Application Bootstrapped Run
  48. Boom. Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  49. Next, let’s lie to people about the classnames they’re using.

    Autoloadin‘ Application Bootstrapped Run
  50. Boom. Facade engaged. Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  51. After that, let’s make sure we can load config files,

    maybe with some sort of callback Autoloadin‘ Application Bootstrapped Run
  52. Oh, look at that. Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  53. Fileloader Handles Laravel config syntax Autoloadin‘ Application Bootstrapped Run

  54. Oh $#@*! Let’s turn on exception handling, we may start

    causing them. Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php
  55. Load the app config Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  56. ...and please stop showing that warning PHP, you jerk. Autoloadin‘

    Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php
  57. Load aliases Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  58. Adds PUT, DELETE methods Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  59. QUICK! We need a place to store ALL our service

    providers! Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php
  60. THE MANIFEST A JSON file that sits in /app/storage/meta Autoloadin‘

    Application Bootstrapped Run
  61. Now, iterate through our registered service providers Calling the boot()

    method on each Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php
  62. AuthServiceProvider::boot() Autoloadin‘ Application Bootstrapped Run Illuminate\Support\ServiceProvider

  63. The Application will also fire any registered call backs upon

    boot completion Autoloadin‘ Application Bootstrapped Run Illuminate\Foundation\Application
  64. Unleash the /app/start/global.php file Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  65. Unleash the /app/start/{$env}.php file Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  66. Get the routes.php file Autoloadin‘ Application Bootstrapped Run /vendor/laravel/framework/src/Illuminate/Foundation/start.php

  67. 1. Set the $env var if we’re running tests 2.

    Setup the Facade 3. Load the config files 4. Set the default timezone 5. Register the alias loader 6. Allow for PUT and DELETE requests with forms 7. Create the provider repository 8. Boot ServiceProviders with Application::boot() 9. Require the /app/start/global.php file 10. Require the /app/start/$env.php file 11. Require the /app/routes.php file Phase 3 Complete Autoloadin‘ Application Bootstrapped Run
  68. Return to /bootstrap/start.php Autoloadin‘ Application Bootstrapped Run

  69. Which returns our $app object to... Autoloadin‘ Application Bootstrapped Run

  70. INDEX.PHP!!!!! For the grand finale Autoloadin‘ Application Bootstrapped Run

  71. Part 4: Run

  72. 1. Run the app 2. Set the session store if

    it exists 3. Find the route 4. Run the route code, calling filters where applicable (ex: before and after) 5. Generate a Response object 6. Send the response 7. Call the after filter Autoloadin‘ Application Bootstrapped Run Phase 4 Commence
  73. Autoloadin‘ Application Bootstrapped Run /public/index.php

  74. Application::run() Autoloadin‘ Application Bootstrapped Run Illuminate\Foundation\Application

  75. Sets the session store Autoloadin‘ Application Bootstrapped Run Illuminate\Foundation\Application Application::prepareRequest()

  76. Finds the route and runs it Autoloadin‘ Application Bootstrapped Run

    Illuminate\Routing\Router Router::dispatch() (Calls the global before filter right before this)
  77. Create a Symfony Response object Autoloadin‘ Application Bootstrapped Run Illuminate\Routing\Router

    Router::prepare()
  78. Sends the response to the browser Autoloadin‘ Application Bootstrapped Run

    Illuminate\Foundation\Application Application::run()
  79. Calls the finish filter Autoloadin‘ Application Bootstrapped Run Illuminate\Routing\Router

  80. Autoloadin‘ Application Bootstrapped Run Illuminate\Routing\Router Phase 4 Complete 1. Run

    the app 2. Set the session store if it exists 3. Find the route 4. Run the route code, calling filters where applicable (ex: before and after) 5. Generate a Response object 6. Send the response 7. Call the after filter
  81. “I’m finished!” @kennymeyers thenerdary.net