Uri Class
The Uri class allows you to interact with the URI.
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();
echo Uri::base(false);
|
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');
echo Uri::create('controller/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'), true);
|
The current method allows you to fetch the current URL, including the base URL inside HMVC pattern.
Static |
Yes |
Parameters |
None |
Returns |
string |
Example |
echo Uri::current();
|
The main method allows you to fetch the current URL, including the base URL.
Static |
Yes |
Parameters |
None |
Returns |
string |
Example |
echo Uri::main();
|
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 |
echo Uri::segment(3);
|
The segments method allows you to fetch the current URI segments in an array.
Static |
Yes |
Parameters |
None |
Returns |
array |
Example |
echo Uri::segments();
|
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 |
echo Uri::segment_replace('http://localhost/*/*/three');
echo Uri::segment_replace('http://localhost/this/that/*/other');
echo Uri::segment_replace('http://localhost/*/*/three', true);
|
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 |
echo Uri::update_query_string(array('three' => 3));
echo Uri::update_query_string(array('two' => 3));
echo Uri::update_query_string(array('four' => 4), 'http://localhost/controller');
echo Uri::update_query_string('three', 3, true);
|
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 |
$arr = Uri::to_assoc();
$arr = Uri::to_assoc();
|
The string method allows you to fetch the current URI.
Static |
Yes |
Parameters |
None |
Returns |
string |
Example |
echo Uri::string();
|
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 |
echo Uri::build_query_string(array('varA' => 'varA'), 'varB', array('varC' => 'varC'));
|