request.php 5.44 KB
Newer Older
1
<?php namespace Laravel;
2

Taylor Otwell committed
3
class Request {
Taylor Otwell committed
4 5

	/**
Taylor Otwell committed
6
	 * All of the route instances handling the request.
7
	 *
Taylor Otwell committed
8
	 * @var array
9
	 */
Taylor Otwell committed
10
	public static $route;
11 12

	/**
Taylor Otwell committed
13
	 * The Symfony HttpFoundation Request instance.
14
	 *
Taylor Otwell committed
15
	 * @var HttpFoundation\Request
16
	 */
Taylor Otwell committed
17
	public static $foundation;
Taylor Otwell committed
18 19

	/**
Taylor Otwell committed
20
	 * The request data key that is used to indicate a spoofed request method.
21 22 23
	 *
	 * @var string
	 */
Taylor Otwell committed
24
	const spoofer = '_method';
25 26

	/**
Taylor Otwell committed
27
	 * Get the URI for the current request.
28
	 *
29
	 * @return string
Taylor Otwell committed
30
	 */
31
	public static function uri()
Taylor Otwell committed
32
	{
Phill Sparks committed
33
		return URI::current();
34 35 36
	}

	/**
37 38 39 40
	 * Get the request method.
	 *
	 * @return string
	 */
41
	public static function method()
42
	{
Taylor Otwell committed
43
		$method = static::foundation()->getMethod();
44

Taylor Otwell committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		return ($method == 'HEAD') ? 'GET' : $method;
	}

	/**
	 * Get a header from the request.
	 *
	 * <code>
	 *		// Get a header from the request
	 *		$referer = Request::header('referer');
	 * </code>
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
	public static function header($key, $default = null)
	{
		return array_get(static::foundation()->headers->all(), $key, $default);
	}

	/**
	 * Get all of the HTTP request headers.
	 *
	 * @return array
	 */
	public static function headers()
	{
		return static::foundation()->headers->all();
73 74 75 76 77 78 79 80 81
	}

	/**
	 * Get an item from the $_SERVER array.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return string
	 */
82
	public static function server($key = null, $default = null)
83
	{
84
		return array_get(static::foundation()->server->all(), strtoupper($key), $default);
85 86 87 88 89 90 91
	}

	/**
	 * Determine if the request method is being spoofed by a hidden Form element.
	 *
	 * @return bool
	 */
92
	public static function spoofed()
93
	{
Taylor Otwell committed
94
		return ! is_null(static::foundation()->get(Request::spoofer));
95 96 97 98 99
	}

	/**
	 * Get the requestor's IP address.
	 *
100
	 * @param  mixed   $default
101 102
	 * @return string
	 */
103
	public static function ip($default = '0.0.0.0')
104
	{
105 106
		$client_ip = static::foundation()->getClientIp();
		return $client_ip === NULL ? $default : $client_ip;
107 108 109
	}

	/**
110 111 112 113 114 115
	 * Get the list of acceptable content types for the request.
	 *
	 * @return array
	 */
	public static function accept()
	{
116
		return static::foundation()->getAcceptableContentTypes();
117 118 119 120 121
	}

	/**
	 * Determine if the request accepts a given content type.
	 *
Sergii Grebeniuk committed
122
	 * @param  string  $type
123 124 125 126 127 128 129 130
	 * @return bool
	 */
	public static function accepts($type)
	{
		return in_array($type, static::accept());
	}

	/**
131 132 133 134 135 136 137 138 139 140
	 * Get the languages accepted by the client's browser.
	 *
	 * @return array
	 */
	public static function languages()
	{
		return static::foundation()->getLanguages();
	}

	/**
141
	 * Determine if the current request is using HTTPS.
142
	 *
143
	 * @return bool
144
	 */
145
	public static function secure()
146
	{
147
		return static::foundation()->isSecure() and Config::get('application.ssl');
148 149 150
	}

	/**
Taylor Otwell committed
151 152 153 154 155 156 157 158
	 * Determine if the request has been forged.
	 *
	 * The session CSRF token will be compared to the CSRF token in the request input.
	 *
	 * @return bool
	 */
	public static function forged()
	{
159
		return Input::get(Session::csrf_token) !== Session::token();
Taylor Otwell committed
160 161 162
	}

	/**
163
	 * Determine if the current request is an AJAX request.
164 165 166
	 *
	 * @return bool
	 */
167
	public static function ajax()
168
	{
169
		return static::foundation()->isXmlHttpRequest();
170 171
	}

172
	/**
173 174 175 176 177 178
	 * Get the HTTP referrer for the request.
	 *
	 * @return string
	 */
	public static function referrer()
	{
Taylor Otwell committed
179
		return static::foundation()->headers->get('referer');
180
	}
181 182 183 184 185 186 187 188
	
	/**
	 * Get the timestamp of the time when the request was started.
	 *
	 * @return int
	 */
	public static function time()
	{
189
		return (int) LARAVEL_START;
190
	}
191 192

	/**
193 194 195 196 197 198 199 200 201 202
	 * Determine if the current request is via the command line.
	 *
	 * @return bool
	 */
	public static function cli()
	{
		return defined('STDIN');
	}

	/**
203 204 205 206 207 208
	 * Get the Laravel environment for the current request.
	 *
	 * @return string|null
	 */
	public static function env()
	{
Taylor Otwell committed
209
		return static::foundation()->server->get('LARAVEL_ENV');
210 211 212
	}

	/**
213 214 215 216 217 218 219 220 221 222 223
	 * Set the Laravel environment for the current request.
	 *
	 * @param  string  $env
	 * @return void
	 */
	public static function set_env($env)
	{
		static::foundation()->server->set('LARAVEL_ENV', $env);
	}

	/**
224 225 226 227 228 229 230 231 232 233 234
	 * Determine the current request environment.
	 *
	 * @param  string  $env
	 * @return bool
	 */
	public static function is_env($env)
	{
		return static::env() === $env;
	}

	/**
235 236 237
	 * Detect the current environment from an environment configuration.
	 *
	 * @param  array        $environments
238
	 * @param  string       $uri
239 240
	 * @return string|null
	 */
241
	public static function detect_env(array $environments, $uri)
242 243 244 245 246
	{
		foreach ($environments as $environment => $patterns)
		{
			// Essentially we just want to loop through each environment pattern
			// and determine if the current URI matches the pattern and if so
247
			// we will simply return the environment for that URI pattern.
248 249
			foreach ($patterns as $pattern)
			{
250
				if (Str::is($pattern, $uri))
251 252 253 254 255 256 257 258
				{
					return $environment;
				}
			}
		}
	}

	/**
259
	 * Get the main route handling the request.
260
	 *
261
	 * @return Route
262
	 */
263
	public static function route()
264
	{
265
		return static::$route;
266
	}
Taylor Otwell committed
267

268
	/**
269 270 271 272 273 274 275 276 277 278
	 * Get the Symfony HttpFoundation Request instance.
	 *
	 * @return HttpFoundation\Request
	 */
	public static function foundation()
	{
		return static::$foundation;
	}

	/**
279 280 281 282 283 284 285 286
	 * Pass any other methods to the Symfony request.
	 *
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return mixed
	 */
	public static function __callStatic($method, $parameters)
	{
287
		return call_user_func_array(array(static::foundation(), $method), $parameters);
288 289
	}

290
}