laravel.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php namespace Laravel;
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Bootstrap The Framework Core
  5. |--------------------------------------------------------------------------
  6. |
  7. | By including this file, the core of the framework will be setup which
  8. | includes the class auto-loader, and the registration of any bundles.
  9. | Basically, once this file has been included, the entire framework
  10. | may be used by the developer.
  11. |
  12. */
  13. require 'core.php';
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Setup Error & Exception Handling
  17. |--------------------------------------------------------------------------
  18. |
  19. | Next we'll register custom handlers for all errors and exceptions so we
  20. | can display a clean error message for all errors, as well as do any
  21. | custom error logging that may be setup by the developer.
  22. |
  23. */
  24. set_exception_handler(function($e)
  25. {
  26. require_once path('sys').'error'.EXT;
  27. Error::exception($e);
  28. });
  29. set_error_handler(function($code, $error, $file, $line)
  30. {
  31. require_once path('sys').'error'.EXT;
  32. Error::native($code, $error, $file, $line);
  33. });
  34. register_shutdown_function(function()
  35. {
  36. require_once path('sys').'error'.EXT;
  37. Error::shutdown();
  38. });
  39. /*
  40. |--------------------------------------------------------------------------
  41. | Report All Errors
  42. |--------------------------------------------------------------------------
  43. |
  44. | By setting error reporting to -1, we essentially force PHP to report
  45. | every error, and this is guaranteed to show every error on future
  46. | releases of PHP. This allows everything to be fixed early!
  47. |
  48. */
  49. error_reporting(-1);
  50. /*
  51. |--------------------------------------------------------------------------
  52. | Start The Application Bundle
  53. |--------------------------------------------------------------------------
  54. |
  55. | The application "bundle" is the default bundle for the installation and
  56. | we'll fire it up first. In this bundle's bootstrap, more configuration
  57. | will take place and the developer can hook into some of the core
  58. | framework events such as the configuration loader.
  59. |
  60. */
  61. Bundle::start(DEFAULT_BUNDLE);
  62. /*
  63. |--------------------------------------------------------------------------
  64. | Auto-Start Other Bundles
  65. |--------------------------------------------------------------------------
  66. |
  67. | Bundles that are used throughout the application may be auto-started
  68. | so they are immediately available on every request without needing
  69. | to explicitly start them within the application.
  70. |
  71. */
  72. foreach (Bundle::$bundles as $bundle => $config)
  73. {
  74. if ($config['auto']) Bundle::start($bundle);
  75. }
  76. /*
  77. |--------------------------------------------------------------------------
  78. | Register The Catch-All Route
  79. |--------------------------------------------------------------------------
  80. |
  81. | This route will catch all requests that do not hit another route in
  82. | the application, and will raise the 404 error event so the error
  83. | can be handled by the developer in their 404 event listener.
  84. |
  85. */
  86. Routing\Router::register('*', '(:all)', function()
  87. {
  88. return Event::first('404');
  89. });
  90. /*
  91. |--------------------------------------------------------------------------
  92. | Gather The URI And Locales
  93. |--------------------------------------------------------------------------
  94. |
  95. | When routing, we'll need to grab the URI and the supported locales for
  96. | the route so we can properly set the language and route the request
  97. | to the proper end-point in the application.
  98. |
  99. */
  100. $uri = URI::current();
  101. $locales = Config::get('application.languages', array());
  102. $locales[] = Config::get('application.language');
  103. /*
  104. |--------------------------------------------------------------------------
  105. | Set The Locale Based On Route
  106. |--------------------------------------------------------------------------
  107. |
  108. | If the URI starts with one of the supported languages, we will set
  109. | the default language to match that URI segment and shorten the
  110. | URI we'll pass to the router to not include the lang segment.
  111. |
  112. */
  113. foreach ($locales as $locale)
  114. {
  115. if (starts_with($uri, $locale))
  116. {
  117. Config::set('application.language', $locale);
  118. $uri = trim(substr($uri, strlen($locale)), '/'); break;
  119. }
  120. }
  121. if ($uri === '') $uri = '/';
  122. /*
  123. |--------------------------------------------------------------------------
  124. | Route The Incoming Request
  125. |--------------------------------------------------------------------------
  126. |
  127. | Phew! We can finally route the request to the appropriate route and
  128. | execute the route to get the response. This will give an instance
  129. | of the Response object that we can send back to the browser
  130. |
  131. */
  132. Request::$route = Routing\Router::route(Request::method(), $uri);
  133. $response = Request::$route->call();
  134. /*
  135. |--------------------------------------------------------------------------
  136. | "Render" The Response
  137. |--------------------------------------------------------------------------
  138. |
  139. | The render method evaluates the content of the response and converts it
  140. | to a string. This evaluates any views and sub-responses within the
  141. | content and sets the raw string result as the new response.
  142. |
  143. */
  144. $response->render();
  145. /*
  146. |--------------------------------------------------------------------------
  147. | Persist The Session To Storage
  148. |--------------------------------------------------------------------------
  149. |
  150. | If a session driver has been configured, we will save the session to
  151. | storage so it is available for the next request. This will also set
  152. | the session cookie in the cookie jar to be sent to the user.
  153. |
  154. */
  155. if (Config::get('session.driver') !== '')
  156. {
  157. Session::save();
  158. }
  159. /*
  160. |--------------------------------------------------------------------------
  161. | Send The Response To The Browser
  162. |--------------------------------------------------------------------------
  163. |
  164. | We'll send the response back to the browser here. This method will also
  165. | send all of the response headers to the browser as well as the string
  166. | content of the Response. This should make the view available to the
  167. | browser and show something pretty to the user.
  168. |
  169. */
  170. $response->send();
  171. /*
  172. |--------------------------------------------------------------------------
  173. | And We're Done!
  174. |--------------------------------------------------------------------------
  175. |
  176. | Raise the "done" event so extra output can be attached to the response.
  177. | This allows the adding of debug toolbars, etc. to the view, or may be
  178. | used to do some kind of logging by the application.
  179. |
  180. */
  181. Event::fire('laravel.done', array($response));