what is URL Segments in codeingiter?

The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can also retrieve information about the re-routed segments.

<>b>Note: This class is initialized automatically by the system so there is no need to do it manually.

$this->uri->segment(n) // // n=1 for controller, n=2 for method, etc

Segment function allow you to retrieve a specific segment form URI string where n is a segment number.Segments are numbered from left to right.

By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that permits you to set your own default value if the segment is missing.

Example :- http://itechtuto.com/codeigniter/blog/news/id

By the above example URI segment function give result by n parameter.

echo $this->uri->segment(1);//it will print codeigniter
echo $this->uri->segment(2);//it will print bolog
echo $this->uri->segment(3);//it will print news
echo $this->uri->segment(4);//it will print id

How To Get Last segment From URL In Codeigniter?

I need to get the value of last URI / URL segment in Codeigniter today and I came up with this easy solutions. It permits you to retrieve a last segment from the URI. ALL three alternative works like charm.
Alternative 1:
$last = end($this->uri->segments);
//Where, segments is a class variable.
Alternative 2:
$last = end($this->uri->segment_array());
Alternative 3:
$total = $this->uri->total_segments();
$last = $this->uri->segment($total);

$this->uri->slash_segment(n)

slash_segment function allow you to retrieve a specific segment form URI string with slash and second parameter add slash to trailing and/or leading point. If the parameter is not used, added slash on trailing point.

echo $this->uri->slash_segment(1);//it will print codeigniter/
echo $this->uri->slash_segment(2,'leading');//it will print /blog
echo $this->uri->slash_segment(3,'both');//it will print /news/

$this->uri->uri_string()

uri_string function use for generating string from complete URI. For example

http://itechtuto.com/index.php/codeigniter/language/php/function
echo $this->uri->uri_string();
//it will return
/**
codeigniter/language/php/function
*/

$this->uri->total_segments()

total_segments function return the total number of segments. For example

echo $this->uri->total_segments();//it will return 4

$this->uri->segment_array()

segment_array function returns an array containing the URI segments. For example

print_r($this->uri->segment_array());

/** it will Produces
Array ( [1] => blog [2] => php[3] =>language [4] =>function )
*/
//Or you can use like

$uri_data= $this->uri->segment_array();
foreach ($uri_data as $segment)
{
echo $segment;
echo '<br />';
}





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here