request.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php namespace Laravel;
  2. class Request {
  3. /**
  4. * All of the route instances handling the request.
  5. *
  6. * @var array
  7. */
  8. public static $route;
  9. /**
  10. * The Symfony HttpFoundation Request instance.
  11. *
  12. * @var HttpFoundation\Request
  13. */
  14. public static $foundation;
  15. /**
  16. * The request data key that is used to indicate a spoofed request method.
  17. *
  18. * @var string
  19. */
  20. const spoofer = '_method';
  21. /**
  22. * Get the URI for the current request.
  23. *
  24. * @return string
  25. */
  26. public static function uri()
  27. {
  28. return URI::current();
  29. }
  30. /**
  31. * Get the request method.
  32. *
  33. * @return string
  34. */
  35. public static function method()
  36. {
  37. $method = static::foundation()->getMethod();
  38. return ($method == 'HEAD') ? 'GET' : $method;
  39. }
  40. /**
  41. * Get a header from the request.
  42. *
  43. * <code>
  44. * // Get a header from the request
  45. * $referer = Request::header('referer');
  46. * </code>
  47. *
  48. * @param string $key
  49. * @param mixed $default
  50. * @return mixed
  51. */
  52. public static function header($key, $default = null)
  53. {
  54. return array_get(static::foundation()->headers->all(), $key, $default);
  55. }
  56. /**
  57. * Get all of the HTTP request headers.
  58. *
  59. * @return array
  60. */
  61. public static function headers()
  62. {
  63. return static::foundation()->headers->all();
  64. }
  65. /**
  66. * Get an item from the $_SERVER array.
  67. *
  68. * @param string $key
  69. * @param mixed $default
  70. * @return string
  71. */
  72. public static function server($key = null, $default = null)
  73. {
  74. return array_get(static::foundation()->server->all(), strtoupper($key), $default);
  75. }
  76. /**
  77. * Determine if the request method is being spoofed by a hidden Form element.
  78. *
  79. * @return bool
  80. */
  81. public static function spoofed()
  82. {
  83. return ! is_null(static::foundation()->get(Request::spoofer));
  84. }
  85. /**
  86. * Get the requestor's IP address.
  87. *
  88. * @param mixed $default
  89. * @return string
  90. */
  91. public static function ip($default = '0.0.0.0')
  92. {
  93. $client_ip = static::foundation()->getClientIp();
  94. return $client_ip === NULL ? $default : $client_ip;
  95. }
  96. /**
  97. * Get the list of acceptable content types for the request.
  98. *
  99. * @return array
  100. */
  101. public static function accept()
  102. {
  103. return static::foundation()->getAcceptableContentTypes();
  104. }
  105. /**
  106. * Determine if the request accepts a given content type.
  107. *
  108. * @param string $type
  109. * @return bool
  110. */
  111. public static function accepts($type)
  112. {
  113. return in_array($type, static::accept());
  114. }
  115. /**
  116. * Get the languages accepted by the client's browser.
  117. *
  118. * @return array
  119. */
  120. public static function languages()
  121. {
  122. return static::foundation()->getLanguages();
  123. }
  124. /**
  125. * Determine if the current request is using HTTPS.
  126. *
  127. * @return bool
  128. */
  129. public static function secure()
  130. {
  131. return static::foundation()->isSecure() and Config::get('application.ssl');
  132. }
  133. /**
  134. * Determine if the request has been forged.
  135. *
  136. * The session CSRF token will be compared to the CSRF token in the request input.
  137. *
  138. * @return bool
  139. */
  140. public static function forged()
  141. {
  142. return Input::get(Session::csrf_token) !== Session::token();
  143. }
  144. /**
  145. * Determine if the current request is an AJAX request.
  146. *
  147. * @return bool
  148. */
  149. public static function ajax()
  150. {
  151. return static::foundation()->isXmlHttpRequest();
  152. }
  153. /**
  154. * Get the HTTP referrer for the request.
  155. *
  156. * @return string
  157. */
  158. public static function referrer()
  159. {
  160. return static::foundation()->headers->get('referer');
  161. }
  162. /**
  163. * Get the timestamp of the time when the request was started.
  164. *
  165. * @return int
  166. */
  167. public static function time()
  168. {
  169. return (int) LARAVEL_START;
  170. }
  171. /**
  172. * Determine if the current request is via the command line.
  173. *
  174. * @return bool
  175. */
  176. public static function cli()
  177. {
  178. return defined('STDIN');
  179. }
  180. /**
  181. * Get the Laravel environment for the current request.
  182. *
  183. * @return string|null
  184. */
  185. public static function env()
  186. {
  187. return static::foundation()->server->get('LARAVEL_ENV');
  188. }
  189. /**
  190. * Set the Laravel environment for the current request.
  191. *
  192. * @param string $env
  193. * @return void
  194. */
  195. public static function set_env($env)
  196. {
  197. static::foundation()->server->set('LARAVEL_ENV', $env);
  198. }
  199. /**
  200. * Determine the current request environment.
  201. *
  202. * @param string $env
  203. * @return bool
  204. */
  205. public static function is_env($env)
  206. {
  207. return static::env() === $env;
  208. }
  209. /**
  210. * Detect the current environment from an environment configuration.
  211. *
  212. * @param array $environments
  213. * @param string $uri
  214. * @return string|null
  215. */
  216. public static function detect_env(array $environments, $uri)
  217. {
  218. foreach ($environments as $environment => $patterns)
  219. {
  220. // Essentially we just want to loop through each environment pattern
  221. // and determine if the current URI matches the pattern and if so
  222. // we will simply return the environment for that URI pattern.
  223. foreach ($patterns as $pattern)
  224. {
  225. if (Str::is($pattern, $uri))
  226. {
  227. return $environment;
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * Get the main route handling the request.
  234. *
  235. * @return Route
  236. */
  237. public static function route()
  238. {
  239. return static::$route;
  240. }
  241. /**
  242. * Get the Symfony HttpFoundation Request instance.
  243. *
  244. * @return HttpFoundation\Request
  245. */
  246. public static function foundation()
  247. {
  248. return static::$foundation;
  249. }
  250. /**
  251. * Pass any other methods to the Symfony request.
  252. *
  253. * @param string $method
  254. * @param array $parameters
  255. * @return mixed
  256. */
  257. public static function __callStatic($method, $parameters)
  258. {
  259. return call_user_func_array(array(static::foundation(), $method), $parameters);
  260. }
  261. }