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])
  • Parameters :
  • $n (int) : Segment index number.
  • $no_result (mixed) : What to return if the searched segment is not found.
  • Returns : Segment value or $no_result value if not found.
  • Returns type : mixed
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:-

Codeigniter URI Class Library

2. Rsegment.

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

rsegment($n[$no_result = NULL])
  • Parameters :
  • $n (int) : Segment index number.
  • $no_result (mixed) : What to return if the searched segment is not found.
  • Returns : Routed segment value or $no_result value if not found.
  • Returns type : mixed

3. Slash segment.

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

slash_segment($n[$where = 'trailing'])
  • Parameters :
  • $n (int) : Segment index number.
  • $where (string) : Where to add the slash (‘trailing’ or ‘leading’).
  • Returns : Segment value, prepended/suffixed with a forward slash, or a slash if not found.
  • Returns type : String
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:-

Codeigniter URI Class Library

4. Slash rsegment.

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

slash_rsegment($n[$where = 'trailing'])
  • Parameters :
  • $n (int) : Segment index number.
  • $where (string) : Where to add the slash (‘trailing’ or ‘leading’).
  • Returns : Routed segment value, prepended/suffixed with a forward slash.
  • Returns type : string

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()]])
  • Parameters :
  • $n (int) : Segment index number.
  • $default (array) : Default values.
  • Returns : Associative URI segments array.
  • Returns type : String
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:-

Codeigniter URI Class Library

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()]])
  • Parameters :
  • $n (int) : Segment index number.
  • $default (array) : Default values.
  • Returns : Associative URI segments array.
  • Returns type : String

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)
  • Parameters :
  • $array (array) : Input array of key/value pairs.
  • Returns : URI string.
  • Returns type : String
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:-

Codeigniter URI Class Library

8. Uri string.

This reference return a string with the complete uri..

uri_string();
  • Parameters :
  • Returns : URI string.
  • Returns type : 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:-

Codeigniter URI Class Library

9. Ruri string.

This reference return re routed uri.

ruri_string()
  • Parameters :
  • Returns : Routed URI string.
  • Returns type : String

10. Total segments.

This reference return total number of segment.

total_segments()
  • Parameters :
  • Returns : Count of URI segments.
  • Returns type : int

11. Total rsegments.

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

total_rsegments()
  • Parameters :
  • Returns : Count of routed URI segments.
  • Returns type : int

12. segment array.

This reference return an array containing the URI segment.

segment_array()
  • Parameters :
  • Returns : URI segments array.
  • Returns type : 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:-

Codeigniter URI Class Library

12. Rsegment array.

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

rsegment_array()
  • Parameters :
  • Returns : Routed URI segments array.
  • Returns type : Array

Advertisements

Add Comment

📖 Read More