laravel.php 6.94 KB
Newer Older
Taylor Otwell committed
1 2
<?php namespace Laravel;

3 4
use Router;

5 6 7 8 9 10 11 12 13 14 15
/*
|--------------------------------------------------------------------------
| Bootstrap The Framework Core
|--------------------------------------------------------------------------
|
| By including this file, the core of the framework will be setup which
| includes the class auto-loader, and the registration of any bundles.
| Basically, once this file has been included, the entire framework
| may be used by the developer.
|
*/
16

17
require 'core.php';
18

19 20 21 22 23 24 25 26 27 28 29
/*
|--------------------------------------------------------------------------
| Setup Error & Exception Handling
|--------------------------------------------------------------------------
|
| Next we'll register custom handlers for all errors and exceptions so we
| can display a clean error message for all errors, as well as do any
| custom error logging that may be setup by the developer.
|
*/

30
set_exception_handler(function($e)
31
{
32
	require_once path('sys').'error'.EXT;
33

34
	Error::exception($e);
35 36
});

37

38
set_error_handler(function($code, $error, $file, $line)
39
{
40
	require_once path('sys').'error'.EXT;
41

42
	Error::native($code, $error, $file, $line);
43 44
});

45

46
register_shutdown_function(function()
47
{
48
	require_once path('sys').'error'.EXT;
49

50
	Error::shutdown();
51 52
});

53 54 55 56 57 58
/*
|--------------------------------------------------------------------------
| Report All Errors
|--------------------------------------------------------------------------
|
| By setting error reporting to -1, we essentially force PHP to report
Chris Berthe committed
59
| every error, and this is guaranteed to show every error on future
60 61 62 63
| releases of PHP. This allows everything to be fixed early!
|
*/

64 65
error_reporting(-1);

66 67 68 69 70 71 72 73 74 75 76 77
/*
|--------------------------------------------------------------------------
| Start The Application Bundle
|--------------------------------------------------------------------------
|
| The application "bundle" is the default bundle for the installation and
| we'll fire it up first. In this bundle's bootstrap, more configuration
| will take place and the developer can hook into some of the core
| framework events such as the configuration loader.
|
*/

78 79
Bundle::start(DEFAULT_BUNDLE);

80 81 82 83 84 85 86 87 88 89 90
/*
|--------------------------------------------------------------------------
| Auto-Start Other Bundles
|--------------------------------------------------------------------------
|
| Bundles that are used throughout the application may be auto-started
| so they are immediately available on every request without needing
| to explicitly start them within the application.
|
*/

91
foreach (Bundle::$bundles as $bundle => $config)
92
{
93
	if ($config['auto']) Bundle::start($bundle);
94 95
}

96 97 98 99 100 101 102 103 104 105 106
/*
|--------------------------------------------------------------------------
| Register The Catch-All Route
|--------------------------------------------------------------------------
|
| This route will catch all requests that do not hit another route in
| the application, and will raise the 404 error event so the error
| can be handled by the developer in their 404 event listener.
|
*/

107
Router::register('*', '(:all)', function()
108 109 110 111
{
	return Event::first('404');
});

112 113
/*
|--------------------------------------------------------------------------
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| Gather The URI And Locales
|--------------------------------------------------------------------------
|
| When routing, we'll need to grab the URI and the supported locales for
| the route so we can properly set the language and route the request
| to the proper end-point in the application.
|
*/

$uri = URI::current();

$languages = Config::get('application.languages', array());

$languages[] = Config::get('application.language');

/*
|--------------------------------------------------------------------------
| Set The Locale Based On The Route
|--------------------------------------------------------------------------
|
| If the URI starts with one of the supported languages, we will set
| the default lagnauge to match that URI segment and shorten the
| URI we'll pass to the router to not include the lang segment.
|
*/

foreach ($languages as $language)
{
Aaron Kuzemchak committed
142
	if (preg_match("#^{$language}(?:$|/)#i", $uri))
143 144 145 146 147 148 149 150 151 152 153 154 155
	{
		Config::set('application.language', $language);

		$uri = trim(substr($uri, strlen($language)), '/'); break;
	}
}

if ($uri == '') $uri = '/';

URI::$uri = $uri;

/*
|--------------------------------------------------------------------------
156 157 158 159 160 161 162 163
| Route The Incoming Request
|--------------------------------------------------------------------------
|
| Phew! We can finally route the request to the appropriate route and
| execute the route to get the response. This will give an instance
| of the Response object that we can send back to the browser
|
*/
Taylor Otwell committed
164

165
Request::$route = Router::route(Request::method(), $uri);
166

Taylor Otwell committed
167 168
$response = Request::$route->call();

169 170
/*
|--------------------------------------------------------------------------
171 172 173 174 175 176 177 178 179 180 181 182 183
| "Render" The Response
|--------------------------------------------------------------------------
|
| The render method evaluates the content of the response and converts it
| to a string. This evaluates any views and sub-responses within the
| content and sets the raw string result as the new response.
|
*/

$response->render();

/*
|--------------------------------------------------------------------------
184 185 186 187
| Persist The Session To Storage
|--------------------------------------------------------------------------
|
| If a session driver has been configured, we will save the session to
Chris Berthe committed
188
| storage so it is available for the next request. This will also set
189 190 191 192
| the session cookie in the cookie jar to be sent to the user.
|
*/

193
if (Config::get('session.driver') !== '')
Taylor Otwell committed
194
{
195
	Session::save();
Taylor Otwell committed
196
}
197

198 199 200 201 202 203 204 205 206 207 208 209
/*
|--------------------------------------------------------------------------
| Send The Response To The Browser
|--------------------------------------------------------------------------
|
| We'll send the response back to the browser here. This method will also
| send all of the response headers to the browser as well as the string
| content of the Response. This should make the view available to the
| browser and show something pretty to the user.
|
*/

210 211
$response->send();

212 213 214 215 216
/*
|--------------------------------------------------------------------------
| And We're Done!
|--------------------------------------------------------------------------
|
Chris Berthe committed
217
| Raise the "done" event so extra output can be attached to the response.
218 219 220 221 222
| This allows the adding of debug toolbars, etc. to the view, or may be
| used to do some kind of logging by the application.
|
*/

223 224
Event::fire('laravel.done', array($response));

225 226 227 228 229 230 231 232 233 234 235 236
/*
|--------------------------------------------------------------------------
| Finish the request for PHP-FastCGI
|--------------------------------------------------------------------------
|
| Stopping the PHP process for PHP-FastCGI users to speed up some
| PHP queries. Acceleration is possible when there are actions in the
| process of script execution that do not affect server response.
| For example, saving the session in memcached can occur after the page
| has been formed and passed to a web server.
*/

237
$response->foundation->finish();