response.php 8.1 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2

3
class Response {
4 5

	/**
6
	 * The content of the response.
7
	 *
8
	 * @var mixed
9
	 */
10
	public $content;
11 12

	/**
13
	 * The HTTP status code of the response.
14
	 *
15 16
	 * @var int
	 */
17
	public $status = 200;
18 19 20 21 22

	/**
	 * The response headers.
	 *
	 * @var array
23
	 */
24
	public $headers = array();
25 26

	/**
27
	 * HTTP status codes.
28
	 *
29 30
	 * @var array
	 */
31
	public static $statuses = array(
32 33 34 35 36 37 38 39 40 41 42 43 44 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 73 74 75 76 77 78 79 80 81 82 83 84 85
		100 => 'Continue',
		101 => 'Switching Protocols',
		200 => 'OK',
		201 => 'Created',
		202 => 'Accepted',
		203 => 'Non-Authoritative Information',
		204 => 'No Content',
		205 => 'Reset Content',
		206 => 'Partial Content',
		207 => 'Multi-Status',
		300 => 'Multiple Choices',
		301 => 'Moved Permanently',
		302 => 'Found',
		303 => 'See Other',
		304 => 'Not Modified',
		305 => 'Use Proxy',
		307 => 'Temporary Redirect',
		400 => 'Bad Request',
		401 => 'Unauthorized',
		402 => 'Payment Required',
		403 => 'Forbidden',
		404 => 'Not Found',
		405 => 'Method Not Allowed',
		406 => 'Not Acceptable',
		407 => 'Proxy Authentication Required',
		408 => 'Request Timeout',
		409 => 'Conflict',
		410 => 'Gone',
		411 => 'Length Required',
		412 => 'Precondition Failed',
		413 => 'Request Entity Too Large',
		414 => 'Request-URI Too Long',
		415 => 'Unsupported Media Type',
		416 => 'Requested Range Not Satisfiable',
		417 => 'Expectation Failed',
		422 => 'Unprocessable Entity',
		423 => 'Locked',
		424 => 'Failed Dependency',
		500 => 'Internal Server Error',
		501 => 'Not Implemented',
		502 => 'Bad Gateway',
		503 => 'Service Unavailable',
		504 => 'Gateway Timeout',
		505 => 'HTTP Version Not Supported',
		507 => 'Insufficient Storage',
		509 => 'Bandwidth Limit Exceeded'
	);

	/**
	 * Create a new response instance.
	 *
	 * @param  mixed  $content
	 * @param  int    $status
	 * @param  array  $headers
86 87
	 * @return void
	 */
88
	public function __construct($content, $status = 200, $headers = array())
89
	{
90 91
		$this->status = $status;
		$this->content = $content;
92
		$this->headers = array_change_key_case($headers);
93 94 95 96 97
	}

	/**
	 * Create a new response instance.
	 *
98 99 100 101 102 103 104 105
	 * <code>
	 *		// Create a response instance with string content
	 *		return Response::make(json_encode($user));
	 *
	 *		// Create a response instance with a given status
	 *		return Response::make('Not Found', 404);
	 *
	 *		// Create a response with some custom headers
106
	 *		return Response::make(json_encode($user), 200, array('header' => 'value'));
107 108
	 * </code>
	 *
109 110
	 * @param  mixed     $content
	 * @param  int       $status
111
	 * @param  array     $headers
112 113
	 * @return Response
	 */
114
	public static function make($content, $status = 200, $headers = array())
115
	{
116
		return new static($content, $status, $headers);
117 118 119
	}

	/**
120
	 * Create a new response instance containing a view.
121
	 *
122 123 124 125 126 127 128 129
	 * <code>
	 *		// Create a response instance with a view
	 *		return Response::view('home.index');
	 *
	 *		// Create a response instance with a view and data
	 *		return Response::view('home.index', array('name' => 'Taylor'));
	 * </code>
	 *
130 131
	 * @param  string    $view
	 * @param  array     $data
132
	 * @return Response
133
	 */
134
	public static function view($view, $data = array())
135
	{
136
		return new static(View::make($view, $data));
137 138 139 140 141 142 143
	}

	/**
	 * Create a new error response instance.
	 *
	 * The response status code will be set using the specified code.
	 *
144
	 * The specified error should match a view in your views/error directory.
145
	 *
146 147 148 149 150 151 152 153
	 * <code>
	 *		// Create a 404 response
	 *		return Response::error('404');
	 *
	 *		// Create a 404 response with data
	 *		return Response::error('404', array('message' => 'Not Found'));
	 * </code>
	 *
154 155
	 * @param  int       $code
	 * @param  array     $data
156
	 * @return Response
157
	 */
158
	public static function error($code, $data = array())
159
	{
160
		return new static(View::make('error.'.$code, $data), $code);
161
	}
162

163 164 165
	/**
	 * Create a new download response instance.
	 *
166 167 168 169 170 171 172 173
	 * <code>
	 *		// Create a download response to a given file
	 *		return Response::download('path/to/file.jpg');
	 *
	 *		// Create a download response with a given file name
	 *		return Response::download('path/to/file.jpg', 'your_file.jpg');
	 * </code>
	 *
174 175 176 177 178
	 * @param  string    $path
	 * @param  string    $name
	 * @param  array     $headers
	 * @return Response
	 */
179
	public static function download($path, $name = null, $headers = array())
180 181 182 183
	{
		if (is_null($name)) $name = basename($path);

		$headers = array_merge(array(
184 185 186 187 188 189 190 191
			'content-description'       => 'File Transfer',
			'content-type'              => File::mime(File::extension($path)),
			'content-disposition'       => 'attachment; filename="'.$name.'"',
			'content-transfer-encoding' => 'binary',
			'expires'                   => 0,
			'cache-control'             => 'must-revalidate, post-check=0, pre-check=0',
			'pragma'                    => 'public',
			'content-length'            => File::size($path),
192 193
		), $headers);

194
		return new static(File::get($path), 200, $headers);
195 196
	}

197
	/**
198
	 * Prepare a response from the given value.
199
	 *
200 201 202 203 204
	 * If the value is not a response, it will be converted into a response
	 * instance and the content will be cast to a string.
	 *
	 * @param  mixed     $response
	 * @return Response
205
	 */
206
	public static function prepare($response)
207
	{
Taylor Otwell committed
208 209 210 211
		if ( ! $response instanceof Response)
		{
			$response = new static($response);
		}
212 213 214 215 216 217 218 219

		// We'll need to force the response to be a string before closing the session,
		// since the developer may be using the session within a view, and we can't
		// age the flash data until the view is rendered.
		//
		// Since this method is used by both the Route and Controller classes, it is
		// a convenient spot to cast the application response to a string before it
		// is returned to the main request handler.
220
		$response->render();
221

222 223 224 225 226 227 228 229 230 231 232
		return $response;
	}

	/**
	 * Convert the content of the Response to a string and return it.
	 *
	 * @return string
	 */
	public function render()
	{
		if (is_object($this->content) and method_exists($this->content, '__toString'))
233
		{
234
			$this->content = $this->content->__toString();
235 236 237
		}
		else
		{
238
			$this->content = (string) $this->content;
239
		}
240

241
		return $this->content;
242 243 244
	}

	/**
245
	 * Send the headers and content of the response to the browser.
246
	 *
247 248 249 250
	 * @return void
	 */
	public function send()
	{
251
		if ( ! headers_sent()) $this->send_headers();
252

253
		echo (string) $this->content;
254 255 256
	}

	/**
257 258
	 * Send all of the response headers to the browser.
	 *
259
	 * @return void
260
	 */
261
	public function send_headers()
262
	{
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		// If the server is using FastCGI, we need to send a slightly different
		// protocol and status header than we normally would. Otherwise it will
		// not call any custom scripts setup to handle 404 responses.
		//
		// The status header will contain both the code and the status message,
		// such as "OK" or "Not Found". For typical servers, the HTTP protocol
		// will also be included with the status.
		if (isset($_SERVER['FCGI_SERVER_VERSION']))
		{
			header('Status: '.$this->status.' '.$this->message());
		}
		else
		{
			header(Request::protocol().' '.$this->status.' '.$this->message());
		}

		// If the content type was not set by the developer, we will set the
		// header to a default value that indicates to the browser that the
		// response is HTML and that it uses the default encoding.
282
		if ( ! isset($this->headers['content-type']))
283
		{
284
			$encoding = Config::get('application.encoding');
Taylor Otwell committed
285

286
			$this->header('content-type', 'text/html; charset='.$encoding);
287
		}
288

289 290
		// Once the framework controlled headers have been sentm, we can
		// simply iterate over the developer's headers and send each one
Taylor Otwell committed
291
		// back to the browser for the response.
292
		foreach ($this->headers as $name => $value)
293 294
		{
			header("{$name}: {$value}", true);
295
		}
296 297 298
	}

	/**
299
	 * Get the status code message for the response.
300
	 *
301 302 303 304 305 306 307 308 309
	 * @return string
	 */
	public function message()
	{
		return static::$statuses[$this->status];
	}

	/**
	 * Add a header to the array of response headers.
310
	 *
311 312
	 * @param  string    $name
	 * @param  string    $value
313 314
	 * @return Response
	 */
315
	public function header($name, $value)
316
	{
317
		$this->headers[strtolower($name)] = $value;
318 319 320
		return $this;
	}

321 322 323 324 325 326 327 328 329 330 331 332
	/**
	 * Set the response status code.
	 *
	 * @param  int       $status
	 * @return Response
	 */
	public function status($status)
	{
		$this->status = $status;
		return $this;
	}

333
}