状态 草稿
Todo Write me
官方最后更新时间 2008/10/06 16:37

请求辅助函数

这是一个检索当前请求的所有各种信息的辅助函数。

方法

referrer()

referrer($default = FALSE) 返回 HTTP referrer,或者如何 referrer 不存在返回默认。参数:

  • [mixed] $default 默认返回 - 默认为 FALSE
  • 如果设置了返回 [mixed] referrer。默认如果通过

实例

echo request::referrer();

返回结果

http://www.google.com/search?hl=en&q=kohana&btnG=Google+Search

is_ajax()

is_ajax() 通过检查 X-Requested-With HTTP request header 来检测但却请求是否是 ajax 请求。:?:

原文:tests if the current request is an AJAX request by checking the X-Requested-With HTTP request header that most popular JS frameworks now set for AJAX calls.

  • 返回 [bool] 如果是 AJAX 请求返回 TRUE,否则返回 FALSE

实例

我们可以使用此方法来检测控制器中的 ajax 请求并关闭视图模板的输出。这样我们就可以返回一个格式化后的 JSON 响应。

if (request::is_ajax())
{
    $this->template->auto_render = FALSE; // 关闭自动显示
    echo json_encode($animals);
}

method()

method() 返回当前 HTTP 请求的方法(GET,POST,PUT,DELETE for example)

  • 返回 [string] 请求方法

实例

echo request::method();

返回结果:

get

accepts()

accepts($type = NULL, $explicit_check = FALSE) 返回检测客户端接受的内容类型(content type)的布尔型。参数:

  • [string] $type 内容类型(content type)
  • [bool] $explicit_check 设置为 TRUE 即关闭自然检测 - 默认为 FALSE
  • 返回 [bool] TRUE 如果客户端接受的内容类型通过。否则返回 FALSE

实例

if (request::accepts('xhtml') && request::accepts('xml') && request::accepts('rss') && request::accepts('atom'))
{
    echo '客户端接受了所有内容类型';
}
else
{
   echo '客户端不能接受 xhtml,xml,rss,atom 的其中一种或多种内容类型';
}

返回结果:

客户端接受了所有内容类型

preferred_accept()

preferred_accept($types, $explicit_check = FALSE) compare the q values for given array of content types and return the one with the highest value. If items are found to have the same q value, the first one encountered in the given array wins. If all items in the given array have a q value of 0, FALSE is returned.

  • [array] $types array of the content types
  • [bool] $explicit_check sets to TRUE to disable wildcard checking - default FALSE
  • returns [mixed] mime type with highest q value, FALSE if none of the given types are accepted

实例

echo request::preferred_accept(array('xhtml', 'xml'));

返回结果:

xhtml

accepts_at_quality()

accepts_at_quality($type = NULL, $explicit_check = FALSE) 返回检测客户端接受的内容类型(content type)的 quality factor。

  • [string] $type 内容类型(例如 “image/jpg”, “jpg”)
  • [bool] $explicit_check 设置为 TRUE 即关闭自然检测 - 默认为 FALSE
  • 返回 [integer|float] quality factor

实例

echo request::accepts_at_quality('image/jpg');

返回结果:

0.8
helpers/request.txt · 最后更改: 2008/11/18 20:11 由 icyleaf