ExpressionEngine® 3 User Guide

Legacy Documentation

You are using the documentation for version 3.5.17. Go here for the latest version or check here for your available upgrades to the latest version.

URI Class

class URI

The URI Class provides methods that help you retrieve information from your URI strings. This class is initialized automatically.


URI::segment($n[, $no_result = FALSE])

Permits you to retrieve a specific segment, where $n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

The segment numbers would be this:

  1. news
  2. local
  3. metro
  4. crime_is_up

By default the method returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, this would tell the method to return the number zero in the event of failure:

$product_id = ee()->uri->segment(3, 0);

It helps avoid having to write code like this:

if (ee()->uri->segment(3) === FALSE)
    $product_id = 0;
    $product_id = ee()->uri->segment(3);
  • $n (integer) – The segment number you want to retrieve
  • $no_result (mixed) – Default to use if the segment does not exist

The segment

Return type:



URI::slash_segment($n[, $where = 'trailing'])

This method is almost identical to URI::segment except it adds a trailing and/or leading slash based on the second parameter. If the parameter is not used, a trailing slash added. Examples:

ee()->uri->slash_segment(3); // returns segment/
ee()->uri->slash_segment(3, 'leading'); // returns /segment
ee()->uri->slash_segment(3, 'both'); // returns /segment/
  • $n (integer) – The segment number you want to retrieve
  • $where (string) – 'trailing' if you want the slash at the end, 'leading' if you want it at the beginning, 'both' if you want both

Segment with a slash

Return type:



URI::uri_to_assoc([$n = 3[, $default = array()]])

This method lets you turn URI segments into an associative array of key/value pairs. Consider this URI:


Using this method you can turn the URI into an associative array with this prototype:

    'name' => 'joe'
    'location'  => 'UK'
    'gender'  => 'male'

The first parameter of the method lets you set an offset. By default it is set to 3 since your URI will normally contain a controller/function in the first and second segments. Example:

$array = ee()->uri->uri_to_assoc(3);
echo $array['name'];

The second parameter lets you set default key names, so that the array returned by the method will always contain expected indexes, even if missing from the URI. Example:

$default = array('name', 'gender', 'location', 'type', 'sort');
$array = ee()->uri->uri_to_assoc(3, $default);

If the URI does not contain a value in your default, an array index will be set to that name with a value of FALSE.

Lastly, if a corresponding value is not found for a given key (if there is an odd number of URI segments) the value will be set to FALSE (boolean).

  • $n (integer) – Starting segment number
  • $default (array) – Array of default values

Key value pair of the URI

Return type:




Takes an associative array and generates a URI string from it. The array keys will be included in the string. Example:

// Produces:  product/shoes/size/large/color/red

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = ee()->uri->assoc_to_uri($array);
  • $array (array) – Associative array containing key and value pairs

Joined array

Return type:




Returns a string with the complete current URI. For example, if this is your current URL:

The method would return this:

Returns:URI String
Return type:String



Returns the total number of segments in the current URI.

Returns:Total number of segments in current URI
Return type:Integer



Returns an array containing the URI segments. For example:

$segs = ee()->uri->segment_array();

foreach ($segs as $segment)
    echo $segment;
    echo '<br />';
Returns:Array of current segments
Return type:Array