Skip to content

Instantly share code, notes, and snippets.

@sadanandkenganal
Last active April 3, 2022 12:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sadanandkenganal/5f60461112b051dde5ab to your computer and use it in GitHub Desktop.
Save sadanandkenganal/5f60461112b051dde5ab to your computer and use it in GitHub Desktop.
Add active class to menu in codeigniter.
@a-tesh
Copy link

a-tesh commented Aug 23, 2019

I ended up with this. An optional method can be passed.

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('active_link')) {
  function activate_menu($controller, $function = null) {
    $CI = get_instance();
    $class = $CI->router->fetch_class();
    if($function) {
        $method = $CI->router->fetch_method();
        return ($method == $function && $class == $controller) ? 'active' : '';
    }
    
    return ($class == $controller) ? 'active' : '';
  }
}?>

@developerinusa
Copy link

developerinusa commented Feb 9, 2020

Hi all, i changed it with to array compatible.

<php if(!function_exists('active_link')) {
    function activate_menu($controller)
    {
        // Getting CI class instance.
        $CI = get_instance();
        // Getting router class to active.
        $class = $CI->router->fetch_class();
        $is_active = FALSE;
        if(is_array($controller)) {
            foreach ($controller as $cont){
               if($cont == $class){
                   $is_active = TRUE;
               }
            }
        }else{
            if($controller == $class){
                $is_active = TRUE;
            }
        }

        return ($is_active) ? "active" : "";
    }
}?>

Yo can also set active class to top menu like this

<?=activate_menu('controller');?>
<?=activate_menu(['controller1', 'controller2']);?>

@zdarin
Copy link

zdarin commented Apr 18, 2020

Hi all, i changed it with to array compatible.

<php if(!function_exists('active_link')) {
    function activate_menu($controller)
    {
        // Getting CI class instance.
        $CI = get_instance();
        // Getting router class to active.
        $class = $CI->router->fetch_class();
        $is_active = FALSE;
        if(is_array($controller)) {
            foreach ($controller as $cont){
               if($cont == $class){
                   $is_active = TRUE;
               }
            }
        }else{
            if($controller == $class){
                $is_active = TRUE;
            }
        }

        return ($is_active) ? "active" : "";
    }
}?>

Yo can also set active class to top menu like this

<?=activate_menu('firma','firmaType');?>

hi i cant figure out why not working in codeigniter 4.0.2 can you help ?

@orbitasol
Copy link

Hi all,
I think it is most easy to track active link by current url with js. Check the following link
Sorry for my bad english.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment