This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoNeeds work on code examples

URI Library

The URI class provides the methods for working with URI's and URI segments. If you use routing it also has methods to work with rerouted URI's.

Note:

This library is initialized automatically by Kohana. No need to do it yourself

Methods

segment()

segment($index = 1, $default = FALSE) retrieves a specific URI segment. Returns $default when the segment does not exist.

//url: http://www.example.com/index.php/article/paris/hilton/

The segments would be:

  1. article
  2. paris
  3. hilton
echo $this->uri->segment(3); // Returns 'hilton'
echo $this->uri->segment(4, 'spears'); // Returns 'spears'

Note: this method also accepts strings. When a string is given as first argument, it will return the segment following the string.

echo $this->uri->segment('article'); // Returns 'paris'
echo $this->uri->segment('paris'); // Returns 'hilton'
echo $this->uri->segment('hilton'); // Returns FALSE

rsegment()

Identical to segment() except that it uses the rerouted URI to retrieve the segments from.

segment_array()

segment_array($offset,$associative) returns an array of all the URI segments

total_segments()

total_segments() returns the number of segments

echo $this->uri->total_segments(); //returns 3

string()

string() returns the entire URI as a string

echo $this->uri->string(); // returns: article/paris/hilton/

last_segment()

last_segment() returns the last segment of an URI

echo $this->uri->last_segment(); // returns: hilton

argument()

argument() returns the requested arguments. This differs from segments as it will only look at the arguments given and skip the controller and method segment.

echo $this->uri->argument(1); // returns: hilton

argument_array()

argument_array() returns an array containing all arguments

echo $this->uri->argument_array(); // returns: array( 'hilton' )

total_arguments()

total_arguments() returns the total number of arguments.

echo $this->uri->total_arguments(); // returns: 1

build_array()

build_array($array, $offset = 0, $associative = FALSE) creates a simple or associative array from an array and an offset. It takes

Returns the corresponding array starting from the defined offset.

Example

print Kohana::debug($this->uri->build_array(array('apple', 'mango', 'pineapple'), 1));
print Kohana::debug($this->uri->build_array(array('fruit1', 'apple', 'fruit2', 'mango', 'fruit3', 'pineapple'), 2, TRUE));

It will return:

Array
(
    [2] => mango
    [3] => pineapple
)
 
Array
(
    [fruit2] => mango
    [fruit3] => pineapple
)