Response クラス

Response クラスは HTTP レスポンスとブラウザ出力を扱うメソッドを含んでいます。

ステータスヘッダを設定する

Fuel において、ステータスヘッダは "特別の" ヘッダとして扱われます。設定の仕方も、他のヘッダとは同じではありません。 コントローラの中で次のように設定します:

$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 Reponse 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);

set_header($name, $value, $replace)

The set_header method allows set custom HTTP headers.

静的 いいえ
パラメータ
パラメータ 規定値 説明
$name 必須 付け加えるヘッダ名。
$value 説明 HTTP ヘッダに付加される必要がある文字列です。
$replace
true
デフォルトでは同名のヘッダの値はすべて置き換えられます。false がセットされた場合は、ヘッダの書き換えはありませんし、書き換え出来ません。
返り値 void
$response = new Response();

// PDF を出力します
$response->set_header('Content-Type', 'application/pdf');

// downloaded.pdf がコールされます
$response->set_header('Content-Disposition', 'attachment; filename="downloaded.pdf"');

// キャッシュをなしにします
$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');

// 更なる例
$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.

get_header($name = null)

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 reponse 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();

send_headers()

The send_headers method sends the set headers out to the browser, including the HTTP status of the request.

静的 いいえ
パラメータ なし
返り値 void
$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).

静的 いいえ
パラメータ
パラメータ 規定値 説明
$send_headers optional If true, send any defined HTTP headers before sending the response body.
Returns void
// 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.