Input Class

The input class allows you to access HTTP parameters, load server variables and user agent details.

uri()

The uri method detects the current URI to build the correct string.

Static Yes
Parameters None
Returns string - the current URI with forward slash prefix
Example
// Example URL: http://localhost/controller/method
echo Input::uri(); // returns /controller/method

json($index = null, $default = null)

The json method returns a json_decoded (and cleaned) value from the json request body.

Static Yes
Parameters
Param Default Description
$index optional The key in the json request body. Multi-dimensional arrays can be accessed by separating the levels by a dot (.)
$default
null
What value should be returned if the array item is not found?
Returns array
Example
$value = \Input::json('key');

// Set a default for when no value is present:
$value = \Input::json('key', 'backup-value');

Note that the hydration code in the Input class uses Format::forge($input, 'json')->to_array(), to be able to access nested data using dot‑notation. This implies that all json objects in the input are converted to arrays instead of StdClass !

xml($index = null, $default = null)

The xml method returns a XML decoded (and cleaned) value from the XML request body.

Static Yes
Parameters
Param Default Description
$index optional The key in the XML request body. Multi-dimensional arrays can be accessed by separating the levels by a dot (.)
$default
null
What value should be returned if the array item is not found?
Returns array - Format::from_json result array
Example
$value = \Input::xml('key');

// Set a default for when no value is present:
$value = \Input::xml('key', 'backup-value');

The cookie method allows you to read $_COOKIE variables. Call without params to fetch the full cookie array.

Static Yes
Parameters
Param Default Description
$index optional The key in the $_COOKIE array. Multi-dimensional arrays can be accessed by separating the levels by a dot (.)
$default
null
What value should be returned if the array item is not found?
Returns mixed
Example
Input::cookie('foo', 'bar');

delete($index = null, $default = null)

The delete method allows you to read parameters from php://input stream when called via DELETE. Call without params to fetch the full array.

Static Yes
Parameters
Param Default Description
$index optional The key in the php://input stream.
$default optional What value should be returned if the array item is not found?
Returns mixed
Example
Input::delete('foo', 'bar');

put($index = null, $default = null)

The put method allows you to read parameters from php://input stream when called via PUT. Call without params to fetch the full array.

Static Yes
Parameters
Param Default Description
$index optional The key in the php://input stream.
$default optional What value should be returned if the array item is not found?
Returns mixed
Example
Input::put('foo', 'bar');

patch($index = null, $default = null)

The patch method allows you to read parameters from php://input stream when called via PATCH. Call without params to fetch the full array.

Static Yes
Parameters
Param Default Description
$index optional The key in the php://input stream.
$default optional What value should be returned if the array item is not found?
Returns mixed
Example
Input::patch('foo', 'bar');

get($index = null, $default = null)

The get method allows you to read $_GET variables. Call without params to fetch the full array.

Static Yes
Parameters
Param Default Description
$index optional The key in the $_GET array. Multi-dimensional arrays can be accessed by separating the levels by a dot (.)
$default optional What value should be returned if the array item is not found?
Returns mixed
Example
Input::get('foo', 'bar');

// You can go multiple levels deep as well.
// This will return $_GET['somevar']['foo']['bar']
Input::get('somevar.foo.bar');

post($index = null, $default = null)

The post method allows you to read $_POST variables. Call without params to fetch the full array.

Static Yes
Parameters
Param Default Description
$index optional The key in the $_POST array. Multi-dimensional arrays can be accessed by separating the levels by a dot (.)
$default optional What value should be returned if the array item is not found?
Returns mixed
Example
Input::post('foo', 'bar');

// You can go multiple levels deep as well.
// This will return $_POST['somevar']['foo']['bar']
Input::post('somevar.foo.bar');

is_ajax()

The is_ajax method returns true if the controller is called via AJAX.

Static Yes
Parameters None
Returns mixed
Example
Input::is_ajax(); // false

method($default = 'GET')

The method method returns the HTTP method used to call the controller. If the header X-HTTP-Method-Override has been sent, the specified method will be returned instead.

Static Yes
Parameters
Param Default Description
$default
'GET'
Default HTTP method.
Returns string
Example
Input::method(); // "GET"

protocol()

The protocol method returns the HTTP protocol used to call the controller.

Static Yes
Parameters None
Returns string
Example
Input::protocol(); // "HTTP"

ip()

The ip method returns the public IP address of the user. If the user operates from behind a proxy server, the IP address of that server is returned.

Static Yes
Parameters None
Returns string
Example
echo Input::ip(); // 73.194.37.104 (public IP address)

real_ip($default = '0.0.0.0', $exclude_reserved = false)

The real_ip method returns the 'real' IP address of the user. If a proxy server is detected, it will attempt to determine the user's private IP address from the information the proxy adds to the header. If this information is not present, the public IP address is returned.

Static Yes
Parameters
Param Default Description
$default
'0.0.0.0'
The value to be returned if no valid IP address was detected.
$exclude_reserved
false
If true, exclude private and reserved IP ranges, and return only public IP addresses.
Returns string
Example
echo Input::real_ip(); // 10.76.12.199 (private IP behind a proxy server)

When $exclude_reserved is true, the following ranges will be excluded from the result:

IPv4:
10.0.0.0/8     // private use network (rfc1918)
172.16.0.0/12  // private use network (rfc1918)
192.168.0.0/16 // private use network (rfc1918)
0.0.0.0/8      // "this" network (rfc1700)
169.254.0.0/16 // link local network (rfc3927)
192.0.2.0/24   // test net (rfc3330)
224.0.0.0/4    // Multicast (rfc3171)
240.0.0.0/4    // Reserved for Future Use (rfc1700)

IPv6:
fc00::/7       // unique-local addresses (rfc4193)
::/128         // unspecified address (rfc4291)
::1/128        // loopback address (rfc4291)
fe80::/10      // link local unicast (rfc4291)
2001:db8::/32  // documentation addresses (rfc3849)
5f00::/8       // 6Bone
3ffe::/16      // 6Bone
::ffff:0:0/96  // IPv4-Mapped addresses (rfc4291)
2001:10::/28   // ORCHID addresses (rfc4843)
::/0           // default unicast route address

server($index = null, $default = null)

The server method allows you to read parameters from the $_SERVER array with default values. Call without params to fetch the full array.

Static Yes
Parameters
Param Default Description
$index optional The key in the $_SERVER array.
$default optional What value should be returned if the array item is not found?
Returns string
Example
Input::server('HTTP_HOST'); // "localhost:8080"

referrer($default = '')

The referrer method allows you to read the referrer from the $_SERVER array.

Static Yes
Parameters
Param Default Description
$default optional What value should be returned if the array item is not found?
Returns string
Example
Input::referrer();

user_agent($default = '')

The user_agent method allows you to read the user agent from the $_SERVER array.

Static Yes
Parameters
Param Default Description
$default optional What value should be returned if the array item is not found?
Returns string
Example
Input::user_agent();

query_string($default = '')

The query_string method allows you to read the query string from the $_SERVER array.

Static Yes
Parameters
Param Default Description
$default optional What value should be returned if the array item is not found?
Returns string
Example
$q = Input::query_string();

all()

The all returns all of the GET, POST, PUT and DELETE variables.

Static Yes
Returns array
Example
Input::all();

file($index = null, $default = null)

The file method allows you to fetch an item from the FILE array.

Static Yes
Parameters
Param Default Description
$index optional The key in the $_FILE array.
$default optional What value should be returned if the array item is not found?
Returns string|array
Example
Input::file();

param($index = null, $default = null)

The param method allows you to fetch an item from either the GET, POST, PUT or DELETE array.

Static Yes
Parameters
Param Default Description
$index optional The key in the $_GET, $_POST, $_PUT or $_DELETE array.
$default optional What value should be returned if the array item is not found?
Returns string|array
Example
Input::param();

extension()

The extension method returns the current URI extension.

Static Yes
Parameters None
Returns string|null
Example
// Example URL: http://localhost/test/
echo Input::extension(); // NULL

// Example URL: http://localhost/test.html
echo Input::extension(); // 'html'

headers($index = null, $default = null)

The headers method returns a specific or all HTTP headers of the current request.

Static Yes
Parameters
Param Default Description
$index optional The name of the HTTP header.
$default optional What value should be returned if the header entry is not found?
Returns array
Example
// check if there was a content length specified
if ($headers = \Input::headers() and array_key_exists('Content-Length', $headers))
{
	echo 'The content length set on this request is', $headers['Content-Length'];
}

// or fetch it directly
if ($header = \Input::headers('Content-Length', false))
{
	echo 'The content length set on this request is', $header;
}