laravel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php namespace Laravel;
  2. /**
  3. * Bootstrap the core framework components like the IoC container and
  4. * the configuration class, and the class auto-loader. Once this file
  5. * has run, the framework is essentially ready for use.
  6. */
  7. require 'core.php';
  8. /**
  9. * Register the default timezone for the application. This will be the
  10. * default timezone used by all date / timezone functions throughout
  11. * the entire application.
  12. */
  13. date_default_timezone_set(Config::get('application.timezone'));
  14. /**
  15. * Register the PHP exception handler. The framework throws exceptions
  16. * on every error that cannot be handled. All of those exceptions will
  17. * be sent through this closure for processing.
  18. */
  19. set_exception_handler(function($e)
  20. {
  21. Error::exception($e);
  22. });
  23. /**
  24. * Register the PHP error handler. All PHP errors will fall into this
  25. * handler which will convert the error into an ErrorException object
  26. * and pass the exception into the exception handler.
  27. */
  28. set_error_handler(function($code, $error, $file, $line)
  29. {
  30. Error::native($code, $error, $file, $line);
  31. });
  32. /**
  33. * Register the shutdown handler. This function will be called at the
  34. * end of the PHP script or on a fatal PHP error. If a PHP error has
  35. * occured, we will convert it to an ErrorException and pass it
  36. * to the common exception handler for the framework.
  37. */
  38. register_shutdown_function(function()
  39. {
  40. Error::shutdown();
  41. });
  42. /**
  43. * Setting the PHP error reporting level to -1 essentially forces
  44. * PHP to report every error, and it is guranteed to show every
  45. * error on future versions of PHP.
  46. *
  47. * If error detail is turned off, we will turn off all PHP error
  48. * reporting and display since the framework will be displaying
  49. * a generic message and we do not want any sensitive details
  50. * about the exception leaking into the views.
  51. */
  52. error_reporting(-1);
  53. ini_set('display_errors', 'Off');
  54. /**
  55. * Load the session using the session manager. The payload will
  56. * be registered in the IoC container as an instance so it can
  57. * be easily access throughout the framework.
  58. */
  59. if (Config::get('session.driver') !== '')
  60. {
  61. Session::start(Config::get('session.driver'));
  62. Session::load(Cookie::get(Config::get('session.cookie')));
  63. IoC::instance('laravel.session', Session::$instance);
  64. }
  65. /**
  66. * Gather the input to the application based on the global input
  67. * variables for the current request. The input will be gathered
  68. * based on the current request method and will be set on the
  69. * Input manager class' static $input property.
  70. */
  71. $input = array();
  72. switch (Request::method())
  73. {
  74. case 'GET':
  75. $input = $_GET;
  76. break;
  77. case 'POST':
  78. $input = $_POST;
  79. break;
  80. case 'PUT':
  81. case 'DELETE':
  82. if (Request::spoofed())
  83. {
  84. $input = $_POST;
  85. }
  86. else
  87. {
  88. parse_str(file_get_contents('php://input'), $input);
  89. }
  90. }
  91. /**
  92. * The spoofed request method is removed from the input so it is not
  93. * unexpectedly included in Input::all() or Input::get(). Leaving it
  94. * in the input array could cause unexpected results if an Eloquent
  95. * model is filled with the input.
  96. */
  97. unset($input[Request::spoofer]);
  98. Input::$input = $input;
  99. /**
  100. * Start all of the bundles that are specified in the configuration
  101. * array of auto-loaded bundles. This lets the developer have an
  102. * easy way to load bundles for every request.
  103. */
  104. foreach (Config::get('application.bundles') as $bundle)
  105. {
  106. Bundle::start($bundle);
  107. }
  108. /**
  109. * Load the "application" bundle. Though the application folder is
  110. * not typically considered a bundle, it is started like one and
  111. * essentially serves as the "default" bundle.
  112. */
  113. Bundle::start(DEFAULT_BUNDLE);
  114. /**
  115. * If the first segment of the URI corresponds with a bundle we'll
  116. * start that bundle. By convention, bundles handle all URIs which
  117. * begin with their bundle name.
  118. */
  119. $bundle = URI::segment(1);
  120. if ( ! is_null($bundle) and Bundle::routable($bundle))
  121. {
  122. Bundle::start($bundle);
  123. }
  124. /**
  125. * Route the request to the proper route in the application. If a
  126. * route is found, the route will be called via the request class
  127. * static property. If no route is found, the 404 response will
  128. * be returned to the browser.
  129. */
  130. if (count(URI::$segments) > 15)
  131. {
  132. throw new \Exception("Invalid request. Too many URI segments.");
  133. }
  134. Request::$route = Routing\Router::route(Request::method(), URI::current());
  135. if ( ! is_null(Request::$route))
  136. {
  137. $response = Request::$route->call();
  138. }
  139. else
  140. {
  141. $response = Response::error('404');
  142. }
  143. /**
  144. * Close the session and write the active payload to persistent
  145. * storage. The session cookie will also be written and if the
  146. * driver is a sweeper, session garbage collection might be
  147. * performed depending on the "sweepage" probability.
  148. */
  149. if (Config::get('session.driver') !== '')
  150. {
  151. Session::save();
  152. }
  153. $response->send();