Request Helper

A helper designed to retrieve all kinds of information on the current request.

Methods

referrer()

referrer($default = FALSE) returns the HTTP referrer, or the default if the referrer is not set. It takes:

  • [mixed] $default default to return - default FALSE
  • returns [mixed] the referrer if it's set, default if passed

Example:

echo request::referrer();

It could result as:

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

is_ajax()

is_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.

  • returns [bool] TRUE if the request is an AJAX one, FALSE otherwise

Example:

We can use this method to detect ajax requests in the controller and disable the view template so we can output a properly formatted JSON response.

$animals = array('dog', 'cat', 'mouse');
if (request::is_ajax())
{
    $this->auto_render = FALSE; // disable auto render
    echo json_encode($animals);
}

method()

method() returns current HTTP request method (GET, POST, PUT, DELETE for example)

  • returns [string] the request method

Example:

echo request::method();

It could result as:

get

accepts()

accepts($type = NULL, $explicit_check = FALSE) returns boolean of whether client accepts content type. It takes:

  • [string] $type the content type
  • [bool] $explicit_check set to TRUE to disable wildcard checking - default FALSE
  • returns [bool] TRUE if the client accepts the content type passed, FALSE otherwise

Example:

if (request::accepts('xhtml') && request::accepts('xml') && request::accepts('rss') && request::accepts('atom'))
{
    echo 'Client accepts all of them';
}
else
{
   echo 'Client doesn\'t accept one/several of xhtml, xml, rss, atom';
}

It could result in HTML as:

Client accepts all of them

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

Example:

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

It could result as:

xhtml

accepts_at_quality()

accepts_at_quality($type = NULL, $explicit_check = FALSE) returns quality factor at which the client accepts content type.

  • [string] $type content type (e.g. “image/jpg”, “jpg”)
  • [bool] $explicit_check sets to TRUE to disable wildcard checking - default FALSE
  • returns [integer|float] the quality factor

Example:

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

It could result as:

0.8
helpers/request.txt · Dernière modification: 15/03/2011 21:17 par alban
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0