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);
The set_header method allows set custom HTTP headers.
This function must called from within a controller method as the response object is one of the controller's properties.
Static |
No |
Parameters |
Param |
Default |
Description |
$name |
Required |
Name of the header to add. Note that names are unique, overwriting each other if the same name is used. |
$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;
|
The send_headers method sends the set headers out to the browser, including the HTTP status of the request.
This function must called from within a controller method as the response object is one of the controller's properties.
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.
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');
|