当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Response - 类別

优质
小牛编辑
117浏览
2023-12-01

Response 类别包含处理 HTTP 回应及浏览器输出的方法。

设定状态表头

在 Fuel 里,状态表头被视为「特别」的表头,你想保留其他表头的话别设定它, 状态表头在控制器里设定如下:

$response = new Response($body, 404);

设定状态表头和其他表头

$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())

forge 方法建立一个 Response 类别的新实例。

静态
参数
参数预设描述
$body
null
回应主体,也就是你想回传给呼叫者的回应。
$status
200
给此回应的 HTTP 状态码。预设值为 200 OK。
$headers
array()
任何应成为该回应一部份的 HTTP 表头。
回传Response 物件
触发'response_created' 事件在物件实例化之后
範例
// 建立一个回应物件,使用一个 View 做为主体,以及一个 404 NOT FOUND 状态码
return Response::forge(View::forge('errors/404page'), 404);

redirect($url = '', $method = 'location', $redirect_code = 302)

redirect 方法提供不同的重导到新 URL 的方法。

静态
参数
参数预设描述
$url
''
要重导向的 URL。
$method
'location'
要使用的重导向方法。支援 'location' 与 'refresh'。
$redirect_code
302
要送回做为重导向一部分的 HTTP 状态码。
回传此方法终止目前的指令码,不回传。
範例
// 使用一个 URL
Response::redirect('http://example.com/home', 'refresh');

// 或使用一个相对 URI
Response::redirect('site/about');

这个方法支援 URL 的万用字元替换。详情请见 Uri::segment_replace()。 你可以透过在你应用程序的 config.php 设定 response.redirect_with_wildcardsfalse 来停用它。

redirect_back($url = '', $method = 'location', $redirect_code = 302)

redirect_back方法能让你重导向回来源页面, 并提供不同的重导到新 URL 的方法。如果前一页不是你应用程序的一部分(也就是说,该使用者来自其他网站), 或没给 URL,它会重导向应用程序首页。

静态
参数
参数预设描述
$url
''
要重导向的 URL。
$method
'location'
要使用的重导向方法。支援 'location' 与 'refresh'。
$redirect_code
302
要送回做为重导向一部分的 HTTP 状态码。
回传此方法终止目前的指令码,不回传。
範例
// 重定向回页面。如果没有 'back',前往到 dashboard 页面
Response::redirect_back('/dashboard', 'refresh');

这个方法支援 URL 的万用字元替换。详情请见 Uri::segment_replace()。 你可以透过在你应用程序的 config.php 设定 response.redirect_with_wildcardsfalse 来停用它。

set_status($status = 200)

set_status 方法能让你更新一个 Response 物件的 HTTP 状态码设定。

静态
参数
参数预设描述
$status
200
给此回应的 HTTP 状态码。预设值为 200 OK。
回传目前物件,鍊结用
範例
$response = new Response();

// 这将是一个 NOT FOUND 回应
$response->set_status(404);

set_header($name, $value, $replace)

set_header 方法可以设定自订的 HTTP 表头。

静态
参数
参数预设描述
$name必要要添加的表头名称。
$value必要需要被添加到 HTTP 表头的字串。
$replace
true
预设所有表头将被同名的值取代, 如果你设定这个参数为 false,表头将不会也不能被覆写。
回传
範例
$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;

注意名称是唯一的,如果相同名称被使用的话将覆写掉其他的。

get_header($name = null)

get_header 方法能让你取回一个先前设定的自订 HTTP 表头。

静态
参数
参数预设描述
$name选择性要取得的表头名称。如果没给,所有表头会被回传。
回传混合。在单一表头的情况下是字串,在所有表头被回传的情况下是阵列。
範例
$response = new Response();
$response->set_header('Pragma', 'no-cache');

// 回传 'no-cache'
$header = $response->get_header('Pragma');

// 回传 array('Pragma' => 'no-cache')
$header = $response->get_header();

body($value = false)

body 方法允许取得目前回应主体,或设定一个新的。

静态
参数
参数预设描述
$value选择性要设定的 Response 主体。如果没给,目前回应主体会被回传。
回传混合。目前回应主体,或给鍊结用的目前回应物件。
範例
$response = new Response();
$response->body('This is the response body');

// 回传 'This is the response body'
$body = $response->body();

send_headers()

send_headers 方法传送设定的表头到浏览器,包含请求的 HTTP 状态。

静态
参数
回传
範例
$response->send_headers();

请注意,通常你不必手动呼叫这个方法,Fuel 会打理好此方法做为请求处理的一部份。

send($send_headers = false)

send 方法发送回应主体到输出缓冲(意即输出)。

静态
参数
参数预设描述
$send_headers选择性如果为 true,在发送回应主体之前发送任何定义的 HTTP 表头。
回传
範例
// 写出任何回应主体
$response->send();

请注意,通常你不必手动呼叫这个方法,Fuel 会打理好此方法做为请求处理的一部份。