Tutorialsplane

Codeigniter URI Class Library


Codeigniter URI Class Library – This library used for parsing the url, and breaking it down into several segment and passed to the controller or store as a variable. It is also used to retrieve information from your URI strings. It is automatically loaded by the system so there is no need to load manually. Here in this tutorial, we are going to explain how to use URI class library.


Codeigniter uri class library

Let us understand how uri class library works in codeigniter with examples.

Class reference:-

There are following references available in uri class library. Now we will explain.

1. Segment.

This reference is used to give Permit you to retrieve a specific segment.

segment($n[$no_result = NULL])
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class uri_controller extends CI_Controller 
{
public function abc()
{
$product_id = $this-?>uri->segment(3, 0);
if ($this->uri->segment(3) === FALSE)
{
echo $product_id = 0;
}
else
{
echo $product_id = $this->uri->segment(2);
}
}
}
?>

Output will be like this:-

2. Rsegment.

This reference is used to retrieve a specific segment from your re-routed URI in the event.

rsegment($n[$no_result = NULL])

3. Slash segment.

This reference is used to add a trailing and leading slash based on the second parameter.

slash_segment($n[$where = 'trailing'])
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class uri_controller extends CI_Controller 
{
public function trailing()
{
echo $this-?>uri->slash_segment(1);
echo $this->uri->slash_segment(2, 'leading');
echo $this->uri->slash_segment(3, 'both');
}
}
}
?>

Output will be like this:-

4. Slash rsegment.

This reference is used to add slashes a specific segment from your re-routed URI.

slash_rsegment($n[$where = 'trailing'])

5. Uri to assoc.

This reference is used to turn uri segment into an associative array of key/values pair.

uri_to_assoc([$n = 3[$default = array()]])
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class uri_controller extends CI_Controller 
{
public function associativeArray()
{
$default = array('name' =?>'Sonu', 'Gender' => 'Male', 'Location' => 'Noida');
$array = $this->uri->uri_to_assoc(3, $default);
print_r($array);
}
}
?>

Output will be like this:-

6. Ruri to assoc.

This reference is used to creates an associative array using the re routed uri.

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

7. Assoc to uri.

This reference is used to take an associative array as input and generate a uri string from this reference.

assoc_to_uri($array)
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class uri_controller extends CI_Controller 
{
public function associativeUri()
{
$array = array('name' =?> 'Sonu', 'Gender' => 'Male', 'Location' => 'Noida');
$str = $this->uri->assoc_to_uri($array);
echo $str;
}
}
?>

Output will be like this:-

8. Uri string.

This reference return a string with the complete uri..

uri_string();
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class uri_controller extends CI_Controller 
{
public function Uristring()
{
echo uri_string();		
}
}
??>

Output will be like this:-

9. Ruri string.

This reference return re routed uri.

ruri_string()

10. Total segments.

This reference return total number of segment.

total_segments()

11. Total rsegments.

This reference return total number of segment in your re routed uri.

total_rsegments()

12. segment array.

This reference return an array containing the URI segment.

segment_array()
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class uri_controller extends CI_Controller 
{
public function segmentArray()
{
$segs = $this-?>uri->segment_array();
foreach ($segs as $segment)
{
echo $segment;
echo '<br/>';
}
}
}
?>

Output will be like this:-

12. Rsegment array.

This reference return the array of segments in your re-routed URI.

rsegment_array()

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference