×

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

Related Topics

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.