request.php 5.2 KB

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