response.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php namespace Laravel;
  2. class Response {
  3. /**
  4. * The content of the response.
  5. *
  6. * @var mixed
  7. */
  8. public $content;
  9. /**
  10. * The HTTP status code of the response.
  11. *
  12. * @var int
  13. */
  14. public $status;
  15. /**
  16. * The response headers.
  17. *
  18. * @var array
  19. */
  20. public $headers = array();
  21. /**
  22. * HTTP status codes.
  23. *
  24. * @var array
  25. */
  26. private $statuses = array(
  27. 100 => 'Continue',
  28. 101 => 'Switching Protocols',
  29. 200 => 'OK',
  30. 201 => 'Created',
  31. 202 => 'Accepted',
  32. 203 => 'Non-Authoritative Information',
  33. 204 => 'No Content',
  34. 205 => 'Reset Content',
  35. 206 => 'Partial Content',
  36. 207 => 'Multi-Status',
  37. 300 => 'Multiple Choices',
  38. 301 => 'Moved Permanently',
  39. 302 => 'Found',
  40. 303 => 'See Other',
  41. 304 => 'Not Modified',
  42. 305 => 'Use Proxy',
  43. 307 => 'Temporary Redirect',
  44. 400 => 'Bad Request',
  45. 401 => 'Unauthorized',
  46. 402 => 'Payment Required',
  47. 403 => 'Forbidden',
  48. 404 => 'Not Found',
  49. 405 => 'Method Not Allowed',
  50. 406 => 'Not Acceptable',
  51. 407 => 'Proxy Authentication Required',
  52. 408 => 'Request Timeout',
  53. 409 => 'Conflict',
  54. 410 => 'Gone',
  55. 411 => 'Length Required',
  56. 412 => 'Precondition Failed',
  57. 413 => 'Request Entity Too Large',
  58. 414 => 'Request-URI Too Long',
  59. 415 => 'Unsupported Media Type',
  60. 416 => 'Requested Range Not Satisfiable',
  61. 417 => 'Expectation Failed',
  62. 422 => 'Unprocessable Entity',
  63. 423 => 'Locked',
  64. 424 => 'Failed Dependency',
  65. 500 => 'Internal Server Error',
  66. 501 => 'Not Implemented',
  67. 502 => 'Bad Gateway',
  68. 503 => 'Service Unavailable',
  69. 504 => 'Gateway Timeout',
  70. 505 => 'HTTP Version Not Supported',
  71. 507 => 'Insufficient Storage',
  72. 509 => 'Bandwidth Limit Exceeded'
  73. );
  74. /**
  75. * Create a new response instance.
  76. *
  77. * @param mixed $content
  78. * @param int $status
  79. * @param array $headers
  80. * @return void
  81. */
  82. public function __construct($content, $status = 200, $headers = array())
  83. {
  84. $this->status = $status;
  85. $this->content = $content;
  86. $this->headers = $headers;
  87. }
  88. /**
  89. * Create a new response instance.
  90. *
  91. * @param mixed $content
  92. * @param int $status
  93. * @param array $headers
  94. * @return Response
  95. */
  96. public static function make($content, $status = 200, $headers = array())
  97. {
  98. return new static($content, $status, $headers);
  99. }
  100. /**
  101. * Create a new response instance containing a view.
  102. *
  103. * @param string $view
  104. * @param array $data
  105. * @return Response
  106. */
  107. public static function view($view, $data = array())
  108. {
  109. return new static(View::make($view, $data));
  110. }
  111. /**
  112. * Create a new response instance containing a named view.
  113. *
  114. * @param string $name
  115. * @param array $data
  116. * @return Response
  117. */
  118. public static function with($name, $data = array())
  119. {
  120. return new static(View::of($name, $data));
  121. }
  122. /**
  123. * Create a new error response instance.
  124. *
  125. * The response status code will be set using the specified code.
  126. *
  127. * Note: The specified error code should correspond to a view in your views/error directory.
  128. *
  129. * @param int $code
  130. * @param array $data
  131. * @return Response
  132. */
  133. public static function error($code, $data = array())
  134. {
  135. return new static(View::make('error/'.$code, $data), $code);
  136. }
  137. /**
  138. * Create a new download response instance.
  139. *
  140. * @param string $path
  141. * @param string $name
  142. * @param array $headers
  143. * @return Response
  144. */
  145. public static function download($path, $name = null, $headers = array())
  146. {
  147. if (is_null($name)) $name = basename($path);
  148. $headers = array_merge(array(
  149. 'Content-Description' => 'File Transfer',
  150. 'Content-Type' => File::mime(File::extension($path)),
  151. 'Content-Disposition' => 'attachment; filename="'.$name.'"',
  152. 'Content-Transfer-Encoding' => 'binary',
  153. 'Expires' => 0,
  154. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
  155. 'Pragma' => 'public',
  156. 'Content-Length' => File::size($path),
  157. ), $headers);
  158. return new static(File::get($path), 200, $headers);
  159. }
  160. /**
  161. * Get the evaluated string contents of the response.
  162. *
  163. * @return string
  164. */
  165. public function render()
  166. {
  167. return ($this->content instanceof View) ? $this->content->render() : (string) $this->content;
  168. }
  169. /**
  170. * Send the response to the browser.
  171. *
  172. * All of the response header will be sent to the browser first, followed by
  173. * the content of the response instance, which will be evaluated and rendered
  174. * by the render method.
  175. *
  176. * @return void
  177. */
  178. public function send()
  179. {
  180. if ( ! isset($this->headers['Content-Type'])) $this->header('Content-Type', 'text/html; charset=utf-8');
  181. if ( ! headers_sent()) $this->send_headers();
  182. echo $this->render();
  183. }
  184. /**
  185. * Send the response headers to the browser.
  186. *
  187. * @return void
  188. */
  189. public function send_headers()
  190. {
  191. $protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
  192. header($protocol.' '.$this->status.' '.$this->statuses[$this->status]);
  193. foreach ($this->headers as $name => $value)
  194. {
  195. header($name.': '.$value, true);
  196. }
  197. }
  198. /**
  199. * Add a header to the response.
  200. *
  201. * @param string $name
  202. * @param string $value
  203. * @return Response
  204. */
  205. public function header($name, $value)
  206. {
  207. $this->headers[$name] = $value;
  208. return $this;
  209. }
  210. /**
  211. * Set the response status code.
  212. *
  213. * @param int $status
  214. * @return Response
  215. */
  216. public function status($status)
  217. {
  218. $this->status = $status;
  219. return $this;
  220. }
  221. /**
  222. * Magic Method for handling the dynamic creation of Responses containing named views.
  223. */
  224. public static function __callStatic($method, $parameters)
  225. {
  226. if (strpos($method, 'with_') === 0)
  227. {
  228. return static::with(substr($method, 5), Arr::get($parameters, 0, array()));
  229. }
  230. }
  231. }