Uri Class

The Uri class allows you to interact with the URI.

base($include_index = true)

The base method return the base URL, excluding the index_file when $include_index is set to false.

Please note: $include_index will be ignored if index_file is not set app/config/config.php

Static Yes
Parameters
Param Default Description
$include_index
true
Whether to append the index to the returned url.
Returns string, the base url with optional index file appended
Example
echo Uri::base();
// http://localhost/index.php

echo Uri::base(false);
// http://localhost/

create($uri = null, $variables = array(), $get_variables = array(), $secure = null)

The create method allows you to create a URL with the given URI, including the base URL.

Static Yes
Parameters
Param Default Description
$uri
null
The URL.
$variables
array()
The variables for the URL.
$get_variables
array()
The query string additions to the URL, its values & keys can use variables from the $variables array.
$secure
null
Set to false to force http, or to true to force https on the created URL
Returns string
Example
echo Uri::create('controller/method');
// returns http://localhost/controller/method

echo Uri::create('controller/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
// returns http://localhost/controller/thing?what=more

echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
// returns http://www.example.org/thing?what=more

echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'), true);
// returns https://www.example.org/thing?what=more

current()

The current method allows you to fetch the current URL, including the base URL inside HMVC pattern.

Static Yes
Parameters None
Returns string
Example
// Example URL: http://localhost/module/controller/method
echo Uri::current(); // returns http://localhost/module/controller/method

main()

The main method allows you to fetch the current URL, including the base URL.

Static Yes
Parameters None
Returns string
Example
// Example URL: http://localhost/controller/method
echo Uri::main(); // returns http://localhost/controller/method

segment($segment, $default = null)

The segment method allows you to return the desired segment, or false if it does not exist.

Static Yes
Parameters
Param Default Description
$segment Required The segment number.
$default
null
The default?
Returns string
Example
// Example URL: http://localhost/controller/method/param1/param2
echo Uri::segment(3); // returns param1

segments()

The segments method allows you to fetch the current URI segments in an array.

Static Yes
Parameters None
Returns array
Example
// Example URL: http://localhost/controller/method
echo Uri::segments(); // returns array(0 => 'controller', 1 => 'method')

segment_replace($url, $secure = null)

The segment_replace method allows you to replace wildcards in a given url by the current URI segment in that position.

Static Yes
Parameters
Param Default Description
$url Required The url containing wildcards. A wildcard is defined by a * character.
$secure
null
If false, the url returned will use HTTP, if true, it will use HTTPS.
Returns string
Throws OutOfBoundsException, if a wildcard is at a segment location for which no value exists.
Example
// Example: Current URL = http://localhost/one/two, with segments 'one' and 'two'

// returns http://localhost/one/two/three
echo Uri::segment_replace('http://localhost/*/*/three');

// throws an exception, as there is no third segment available
echo Uri::segment_replace('http://localhost/this/that/*/other');

// returns https://localhost/one/two/three
echo Uri::segment_replace('http://localhost/*/*/three', true);

update_query_string($vars = array(), $uri = null, $secure = null)

The update_query_string method allows you to update the query string in the current or passed URI.

Static Yes
Parameters
Param Default Description
$vars
array()
An assoc array of query variables, or a string containing the variable name.
$uri
null
The url to use. If not given, the current url will be used. If $vars is a string, this will contain the variables value (which implies you can't pass a custom uri in that case!).
$secure
null
If false, the url returned will use HTTP, if true, it will use HTTPS.
Returns string
Example
// Example: Current URL = http://localhost/test?one=1&two=2

// returns http://localhost/test?one=1&two=2&three=3
echo Uri::update_query_string(array('three' => 3));

// returns http://localhost/test?one=1&two=3
echo Uri::update_query_string(array('two' => 3));

// returns http://localhost/controller?four=4
echo Uri::update_query_string(array('four' => 4), 'http://localhost/controller');

// returns https://localhost/test?one=1&two=2&three=3
echo Uri::update_query_string('three', 3, true);

to_assoc($start = 1)

The to_assoc method takes the segments array and turns it into an associative array if the number of segments is even. If the number of segments is odd, the last array key will have a value of null.

Static Yes
Parameters
Param Default Description
$start
1
The segment number to start with. This allows you to strip leading segments.
Returns array
Example
// Uri is /welcome/index/page/4
$arr = Uri::to_assoc();
/*
Result:
array(
    'welcome' => 'index',
    'page'    => 4,
);
*/

// Uri is /welcome
$arr = Uri::to_assoc();
/*
Result:
array(
    'welcome' => null,
);
*/

string()

The string method allows you to fetch the current URI.

Static Yes
Parameters None
Returns string
Example
// Example URL: http://localhost/controller/method
echo Uri::string(); // returns controller/method

build_query_string($values ...)

The build_query_string method allows you to generate a query string from one or more passed assoc array or string values. You can pass in Input::get() to construct an updated query string from the existing one. If you pass a string, it will be assumed to be a switch, and will be converted to "string=1".

Static Yes
Parameters
Param Default Description
$values Required One or more associative array's or string values
Returns string
Example
// returns "varA=varA&varB=1&varC=varC"
echo Uri::build_query_string(array('varA' => 'varA'), 'varB', array('varC' => 'varC'));