laravel.php 6.9 KB

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