CodeIgniter URI Class

URI Class

The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this library class, so you do not manually load it into a controller file.

Class Reference

There are various class references available in the URI class:

  1. segment(): A segment is the partition of a URI string that permits you to get a specific segment. The segment number begins from left to right of the URI string, for example, https://example.com/index.php/tutorial/and/example/codeigniter/uri_class

The segment number would follow this pattern:

  1. tutorial
  2. and
  3. example
  4. codeigniter
  5. uri_class

Syntax

segment ($n [, $no_result = NULL ] ) 

$n: Here, ‘n’ is the segment number that you want to retrieve from the URI string.

$no_result: It is an optional parameter that allows you to return a value if the requested segment is not found.

Example: Create a Uri_controller.php controller file and save it to the following path application/controller/Uri_controller.php folder. After that, write the following program in the controller file.

Uri_controller.php

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
 class Uri_controller extends CI_Controller 
 {
     public function get_segment()
     {   
       echo "<title> Tutorial and Example </title>"; 
          $data = "Something is missing";
         $get_id = $this->uri->segment(3, $data); /* if the defined segment is not exist, it returns default ($info) string. */
 if ($get_id == TRUE)
 {
 echo "You can also create an additoinal segments at run time by adding segment to the URL  <b >".$get_id."</b>"; 
 }
 else
 {
 echo $data;
 }
 } 
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Uri_controller/get_segment; after that, you can add some segment such as hello/My/Friend,as shown in the below image.

CodeIgniter URI Class
  • rsegment(): The rsegment() function is similar to the segment() function; moreover it enables you to fetch a specific segment from the re-routes string.

Syntax

rsegment ($n [, $no_result = NULL ])

It has two parameters:

$n: Here, ‘n’ is the segment number that you want to retrieve from the re-routed URI string.

$no_result(): In this parameter, you can pass a string if the searched segment is not found.

  • slash_segment(): A slash_segment() function is used to add the slash (‘trailing’ or ‘leading’) based on the followed parameter. If the parameter is not available, it adds a trailing slash to the last parameter.

Syntax

slash_segment ($n [, $no_result = ‘trailing’ ])

$n: It defines the segment index number.

$where: It defines where you want to add ‘trailing’ or ‘leading’ slashes to the segment.

Example: Create a Uri_controller.php file at the defined application/controller folder.

Uri_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Uri_controller extends CI_Controller 
 {
 public function USe_slash()
     {   
       echo "<title> Tutorial and Example </title>";
         echo "<h3> Uses of Slash </h3>"; 
       echo  $this->uri->slash_segment(2)."<br>";
         echo  $this->uri->slash_segment(1, 'trailing')."<br>";
        echo $this->uri->slash_segment(3, 'both')."<br>";
        echo $this->uri->slash_segment(5, 'leading')."<br>"; 
     }
 }
 ?> 

Now execute the following program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Uri_controller/Use_slash; after that, you can add some segment such as hello/My/Friend,as shown in the below image.

CodeIgniter URI Class
  • slash_rsegment(): It is similar to the slash_segment() function, except that it allows you to add slashes to a specific re-routed URI segment.

Syntax

slash_rsegment ($n [, $where = ‘trailing’])

$n: It defines the segment index number.

$where: It defines where you want to add ‘trailing’ or ‘leading’ slashes to the rerouted segment.

  • uri_to_assoc(): As the name suggests, this function can transform a URI segment into an associative array of string in (key/value) pairs.

Syntax

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

In this function, the first parameter represents the segment index number, which takes ‘3’ as a default number. Whereas, the second parameter contains an array that returns the expected key’s value. Also, if the URI has no default, it returns a NULL value.

Example: Create a Uri_controller.php file at the defined application/controller folder.

Uri_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Uri_controller extends CI_Controller 
 {
 public function uri_assoc()
     {
    echo "<title> Tutorial and Example </title>"; 
         echo " <b> Example of uri_to_assoc() function </b> <br />";
         $default = array('name' => 'James', 'Country' => 'USA', 'Occupation' => 'Student', 'sex' => 'Male');
         $data = $this->uri->uri_to_assoc(2, $default);
        print_r($data);
     }
 }
 ?> 

Now execute the following program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Uri_controller/Uri_assoc; after that, add /123 as a new segment,as shown in the below image.

CodeIgniter URI Class
  • ruri_to_assoc(): The ruri_to_assoc() method is similar to the uri_to_assoc() function for creating an associative array but, it uses a re-routed URI segment to build an array.

Syntax

ruri_to_assoc ($n = 3 [, $default = array() ]])

$n: It defines the segment index number

$default: It contains the default array values.

  • assoc_to_uri(): The assoc_to_uri() function is used to create a URI string from an associative array as input that contains key/value pairs.

Syntax

assoc_to_uri ($array)

$array: It takes input as an array of key/value pairs.

Example: Create a Uri_controller.php file at the defined application/controller folder.

Uri_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Uri_controller extends CI_Controller 
 {
 public function assoc()
     {
      echo "<title> Tutorial and Example </title>";
         echo "<h3> Assoc to URI Example </h3>"; 
          $default = array('name' => 'James', 'Country' => 'USA', 'Occupation' => 'Student', 'sex' => 'Male');
         $data = $this->uri->assoc_to_uri($default);
        print_r($data);
     }
 }
 ?> 

Now execute the following program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Uri_controller/assoc, it shows the output as shown below.

CodeIgniter URI Class
  • uri_string(): A uri_to_string() function is used to return the complete URI string. For example, the full URL is http://example.in/index.php/welcome/to/the/world,

The above method will show the URI like this: welcome/to/the/world

  • ruri_string(): It is similar to the uri_to_string() function, but it uses the re-routed URI to return the full routed string.

Example of uri_string() and ruri_string() function

Create a Uri_controller.php file in the application/controller folder.

Uri_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Uri_controller extends CI_Controller 
 {
 public function uristring()
     {   
 echo "<title> Tutorial and Example </title>"; 
         echo "<b> Example of uri_string() function </b> <br />"; 
         $data = $this->uri->uri_string(); // it is uri_string()
         echo $data."<br/>";
         echo "<b> Example of ruri_string() function </b> <br />";
              $info = $this->uri->ruri_string(); // it is ruri_string()
         echo $info."</br>";
 }
 }
 ?> 

Now execute the following program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Uri_controller/uristring, after that add hello/123 as new segmentt, as shown below.

CodeIgniter URI Class
  1. total_segments(): As the name defines, the total_segments() function is used to return the total number of routed URI segments.
  1. total_rsegment(): It is similar to the total_rsegment() function, but is used to count the total number of re-routed URI segments.
  1. segment_array(): As the name suggests, a segment_array() function is used to return the URI segment array.
  1. rsegment_array(): The rsegment_array() function is used to return an array that containing re-route segments.

Example: Create a Uri_controller.php file in the application/controller folder.

Uri_controller.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Uri_controller extends CI_Controller 
 {
 public function count()
     {
         echo "<title> Tutorial and Example </title>";  
         echo "<b> Example of total_segments(), total_rsegments(), segment_array() and rsegment_array() function </b> <br />";
          echo "total_segments count  " .$this->uri->total_segments(). "<br />"; // count total no. of segments
          echo "total_rsegments count  " .$this->uri->total_rsegments()."<br />";
          $string = $this->uri->segment_array(); // it is segment_array()
          echo "<b> segment_array() function </b>";
          foreach ($string as $row) 
          {
              echo $row. "<br />";
          }
          $string2 = $this->uri->rsegment_array();  // it is rsegment_array()
          echo "<b> rsegment_array() function </b>";
          print_r($string2); 
     }
 }
 ?> 

Now execute the following program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Uri_controller/count; after that, add 123 as a new segment, as shown below.

CodeIgniter URI Class