request.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * @return bool
  109. */
  110. public static function accepts($type)
  111. {
  112. return in_array($type, static::accept());
  113. }
  114. /**
  115. * Get the languages accepted by the client's browser.
  116. *
  117. * @return array
  118. */
  119. public static function languages()
  120. {
  121. return static::foundation()->getLanguages();
  122. }
  123. /**
  124. * Determine if the current request is using HTTPS.
  125. *
  126. * @return bool
  127. */
  128. public static function secure()
  129. {
  130. return static::foundation()->isSecure() and Config::get('application.ssl');
  131. }
  132. /**
  133. * Determine if the request has been forged.
  134. *
  135. * The session CSRF token will be compared to the CSRF token in the request input.
  136. *
  137. * @return bool
  138. */
  139. public static function forged()
  140. {
  141. return Input::get(Session::csrf_token) !== Session::token();
  142. }
  143. /**
  144. * Determine if the current request is an AJAX request.
  145. *
  146. * @return bool
  147. */
  148. public static function ajax()
  149. {
  150. return static::foundation()->isXmlHttpRequest();
  151. }
  152. /**
  153. * Get the HTTP referrer for the request.
  154. *
  155. * @return string
  156. */
  157. public static function referrer()
  158. {
  159. return static::foundation()->headers->get('referer');
  160. }
  161. /**
  162. * Get the timestamp of the time when the request was started.
  163. *
  164. * @return int
  165. */
  166. public static function time()
  167. {
  168. return (int) LARAVEL_START;
  169. }
  170. /**
  171. * Determine if the current request is via the command line.
  172. *
  173. * @return bool
  174. */
  175. public static function cli()
  176. {
  177. return defined('STDIN');
  178. }
  179. /**
  180. * Get the Laravel environment for the current request.
  181. *
  182. * @return string|null
  183. */
  184. public static function env()
  185. {
  186. return static::foundation()->server->get('LARAVEL_ENV');
  187. }
  188. /**
  189. * Set the Laravel environment for the current request.
  190. *
  191. * @param string $env
  192. * @return void
  193. */
  194. public static function set_env($env)
  195. {
  196. static::foundation()->server->set('LARAVEL_ENV', $env);
  197. }
  198. /**
  199. * Determine the current request environment.
  200. *
  201. * @param string $env
  202. * @return bool
  203. */
  204. public static function is_env($env)
  205. {
  206. return static::env() === $env;
  207. }
  208. /**
  209. * Detect the current environment from an environment configuration.
  210. *
  211. * @param array $environments
  212. * @param string $uri
  213. * @return string|null
  214. */
  215. public static function detect_env(array $environments, $uri)
  216. {
  217. foreach ($environments as $environment => $patterns)
  218. {
  219. // Essentially we just want to loop through each environment pattern
  220. // and determine if the current URI matches the pattern and if so
  221. // we will simply return the environment for that URI pattern.
  222. foreach ($patterns as $pattern)
  223. {
  224. if (Str::is($pattern, $uri))
  225. {
  226. return $environment;
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * Get the main route handling the request.
  233. *
  234. * @return Route
  235. */
  236. public static function route()
  237. {
  238. return static::$route;
  239. }
  240. /**
  241. * Get the Symfony HttpFoundation Request instance.
  242. *
  243. * @return HttpFoundation\Request
  244. */
  245. public static function foundation()
  246. {
  247. return static::$foundation;
  248. }
  249. /**
  250. * Pass any other methods to the Symfony request.
  251. *
  252. * @param string $method
  253. * @param array $parameters
  254. * @return mixed
  255. */
  256. public static function __callStatic($method, $parameters)
  257. {
  258. return call_user_func_array(array(static::foundation(), $method), $parameters);
  259. }
  260. }