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

$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

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

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

$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

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

$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

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

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

$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

$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

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

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

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

Pin It on Pinterest

Share This