Response Class
The response class contains the methods to deal with HTTP response and browser output.
Setting the Status Header
The status header is treated as a "special" header in Fuel. You do not set it as you would
other headers. The status header is set as follows in the controller:
$response = new Response($body, 404);
Setting the Status Header and other headers
$headers = array (
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
'Pragma' => 'no-cache',
);
$response = new Response($body, 404, $headers);
forge($body = null, $status = 200, array $headers = array())
The forge method creates a new instance of the Response class.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$body |
null
|
The response body, i.e. the response you want to return to the caller. |
$status |
200
|
HTTP status code for this response. Defaults to 200 OK. |
$headers |
array()
|
Any HTTP headers that should be part of the response. |
|
Returns |
Response object |
Triggers |
Event 'response_created' after object instantiation |
Example |
// create a response object, use a View as body, and a 404 NOT FOUND status code
return Response::forge(View::forge('errors/404page'), 404);
|
redirect($url = '', $method = 'location', $redirect_code = 302)
The redirect method offers different methods of redirecting to a new URL.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$url |
''
|
URL to redirect to. |
$method |
'location'
|
Redirection method to use. Supported are 'location' and 'refresh'. |
$redirect_code |
302
|
HTTP status code to send back as part of the redirect. |
|
Returns |
This method terminates the current script, it does not return. |
Example |
// use a URL
Response::redirect('http://example.com/home', 'refresh');
// or use a relative URI
Response::redirect('site/about');
|
This method supports wildcard replacement in the URL. See Uri::segment_replace() for more information.
set_status($status = 200)
The set_status method allows you to update the HTTP status code set on a Response object.
Static |
No |
Parameters |
Param |
Default |
Description |
$status |
200
|
HTTP status code for this response. Defaults to 200 OK. |
|
Returns |
Current object, for chaining |
Example |
$response = new Response();
// This will be a NOT FOUND response
$response->set_status(404);
|
The set_header method allows set custom HTTP headers.
Static |
No |
Parameters |
Param |
Default |
Description |
$name |
Required |
Name of the header to add. |
$value |
Required |
The string that needs to be added to the HTTP header. |
$replace |
true
|
All headers will replace any previous value for the same name by default. If you set
this parameter to false the header won't overwrite and can't be overwritten. |
|
Returns |
void |
Example |
$response = new Response();
// We'll be outputting a PDF
$response->set_header('Content-Type', 'application/pdf');
// It will be called downloaded.pdf
$response->set_header('Content-Disposition', 'attachment; filename="downloaded.pdf"');
// Set no cache
$response->set_header('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate');
$response->set_header('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
$response->set_header('Pragma', 'no-cache');
// More examples
$response->set_header('Content-Language', 'en');
$response->set_header('Content-Type', 'text/html; charset=utf-8');
return $response;
|
Note that names are unique, overwriting each other if the same name is used.
The get_header method allows you to retrieve a custom HTTP headers set previously.
Static |
No |
Parameters |
Param |
Default |
Description |
$name |
optional |
Name of the header to get. If not given, all headers are returned. |
|
Returns |
Mixed. String in case of a single header, Array in case all headers are returned. |
Example |
$response = new Response();
$response->set_header('Pragma', 'no-cache');
// returns 'no-cache'
$header = $response->get_header('Pragma');
// returns array('Pragma' => 'no-cache')
$header = $response->get_header();
|
body($value = false)
The body method allows get the current response body, or set a new one.
Static |
No |
Parameters |
Param |
Default |
Description |
$value |
optional |
Response body to set. If not given, the current response body is returned. |
|
Returns |
Mixed. Current response body, or the current response object for chaining. |
Example |
$response = new Response();
$response->body('This is the response body');
// returns 'This is the response body'
$body = $response->body();
|
The send_headers method sends the set headers out to the browser, including the HTTP status of the request.
Static |
No |
Parameters |
None |
Returns |
void |
Example |
$response->send_headers();
|
Note that you normally don't have to call this method manually. Fuel will take care of this as part of processing the request.
send($send_headers = false)
The send method sends the response body to the output buffer (i.e. it is echo'd out).
Static |
No |
Parameters |
Param |
Default |
Description |
$send_headers |
optional |
If true, send any defined HTTP headers before sending the response body. |
|
Returns |
void |
Example |
// echo whatever the response body is
$response->send();
|
Note that you normally don't have to call this method manually. Fuel will take care of this as part of processing the request.