Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current URI Segment in CodeIgniter

What would be the best way to check for the current URI segment in a CodeIgniter view? What I am trying to do is use the current URI segment [i.e. $this->uri->segment(1)] in order to highlight the current page on the navigation bar.

The best that I have figured out is to do

$data['current_uri'] = $this->uri->segment(1);
$this->load->view('header', $data);

in each of my controllers and then in the header.php file, I check the $current_uri variable to determine which part of the navigation should be highlighted. As you know, this is a very tedious way of doing it, but I'm at a loss of a better way to do this.

It may even be possible to extend the default Controller class to pass the current URI segment, but I'm not sure if this would work, or even how to go about doing it.

like image 507
Jared Avatar asked Sep 09 '10 03:09

Jared


People also ask

What is URI -> segment in CodeIgniter?

$this->uri->segment(n) 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. For example,if your URI like. By the above example URI segment function give result by n parameter.

What do you call the 1st segment of the URI?

The first URI segment controller. It is the process of redirecting or remapping a. controller class or method. Routing. Andi Gutmans was the inventor of PHP.

How can I get segment in PHP?

Get URI Segments in PHP Use the following code to get URL segments of the current URL. It returns all the segments of the current URL as an array. $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

What is URL Helper in CodeIgniter?

CodeIgniter's URL helpers are groups of utility functions which will help you to call ,create and maintain url. It mainly have more than 20 helpers some of them you might be familiar with are URL, email, form etc. These are some common helper functions that generaly used in web based application for email, files, URLs.


4 Answers

I myself use an extra function similar to anchor(). I call it active_anchor(), and it takes all the same parameters plus another (the uri). The function then adds the class 'active' if the uri string passed matches the active_anchor() url paramter.

Then the function returns using the anchor function (all that the function did was determine if the link needed the class 'active' or not.

EDIT:

I just put this code in a file called 'MY_url_helper.php'. That way, when the url helper is loaded (I actually auto load that one since pretty much all of my views use it anyway.) This is just some quick code too, pretty sure it would work. Basically it takes the same arguments as the anchor() function, but also the $key variable. It appends a class of "active" to the anchor tag if the key and url match.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('active_anchor'))
{
    function active_anchor($url = NULL, $title = NULL, $key = NULL, $params = array())
    {
        if ($url && $key)
        {
            if($key == $url)
            {
                if (array_key_exists ('class' , $params))
                {
                    $params['class'] .= ' active';
                }
                else
                {
                    $params['class'] = 'active';
                }
            }
        }
        return anchor($url, $title, $params);
    }
}
like image 57
Matthew Avatar answered Oct 15 '22 09:10

Matthew


Simple way to check the uri segment in view, 

Add some code if matches found.
 <li class="<?php echo (strcmp($this->uri->segment(2),'test')==0)?'active':''; ?>"><li>
 <li class="<?php echo (strcmp($this->uri->segment(2),'test1')==0)?'active':''; ?>"><li>
 <li class="<?php echo (strcmp($this->uri->segment(2),'test2')==0)?'active':''; ?>"><li>
like image 34
prash.patil Avatar answered Oct 15 '22 08:10

prash.patil


I also had the same problem when I was building a customer website in Cakephp, passing those strings for every menu item from controller to view, then checking again in view for implementing the highlighting to tedious to say the least.

For some of my projects now, I have been implementing the same by storing the page information for each of the navigation menu pages in database, things like page name, url, title, position in navigation menu etc.

Then at the start of controller, I store all this data in an array say $pageinfo.

I handle the navigation functionality via a single controller that checks the URI segment and loads the content based on that.

The highlighting part is left to an if statement when generating the view, where I compare each page name to the information I dumped in $pageinfo.

Something like this...

foreach ($navi_menu as $links) {
    if ( $links['title'] == $pageinfo['title'] ) {
      // Highlight here
    }
    else {
      // No highlight
    }
}

This way I don't need to pass the string constants (uri segments in your case) to the view. This CMS-kinda-approach allows me to flexible in adding further items to my menu, without adding more code.

I remember getting this from a codeigniter wiki, can't find the link to it right now.

like image 2
vikmalhotra Avatar answered Oct 15 '22 10:10

vikmalhotra


this simple way and running well for me..

<li class="<?=($this->uri->segment(2)==='test')?'current-menu-item':''?>"><?php echo     anchor ('home/index','HOME'); ?></li>

<li class="<?=($this->uri->segment(2)==='blog')?'current-menu-item':''?>"><?php echo     anchor ('home/blog','BLOG'); ?></li>
<li class="<?=($this->uri->segment(2)==='bla..bla')?'current-menu-item':''?>"><?php     echo anchor ('home/blog','bla..bla'); ?></li>

uri_segment(2) that mean function in ur controller.

but have a weakness, i have trouble if i put view in index controller, so im not use function index ( toruble in uri segment, make 2 current page in same time... read this http://ellislab.com/codeigniter/user-guide/libraries/uri.html

like image 2
Prabu Karana Avatar answered Oct 15 '22 09:10

Prabu Karana