#Add active class to menu in codeigniter
In codeigniter, it’s very easy to add active class to the current menu in menu item.
Follow the below steps.
Step 1:- Create a file called "menu_helper.php" inside "application/helpers/menu_helper.php" and paste the below code.
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
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();
return ($class == $controller) ? 'active' : '';
}
}?>
Step 2:- Load above helper in your controller (go to controllers/yourcontrooller.php). OR Load above helper in .autoload (go to config/autoload.php and add below code).
$this->load->helper('menu');
Step 3 :-. Add class to your menu. pass your controller name in activate_menu() method.
<li class="<?php echo activate_menu('home'); ?>"><a href="<?php echo site_url();?>">Home</a></li>
<li class="<?php echo activate_menu('aboutus'); ?>"><a href="<?php echo site_url('aboutus');?>">About us</a></li>
this code is good but limited, its only looking a class not the method.