sadanandkenganal / activelink_codeigniter.
md
Last active 3 years ago • Report abuse
Code Revisions 13 Stars 5 Forks 2 Embed Download ZIP
Add active class to menu in codeigniter.
activelink_codeigniter.md
#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();
<li class="<?php echo activate_menu('aboutus'); ?>"><a href="<?php echo site_url
peterson-umoke commented on Jan 28, 2017
this code is good but limited, its only looking a class not the method.
peterson-umoke commented on Jan 28, 2017 • edited
<?php
if(!function_exists("show_current_class")) {
function show_current_class($args = "",$class = "active") {
$route_obj = new CI_Router;
$get_page = $route_obj->method;
if($get_page == $args) {
echo $class;
}
}
}
?>
this might be okay
chrismartinez99 commented on Feb 8, 2017
This is great! Thank you! I've modified it a bit here based upon the uri_string. Here you go:
function active_link($path, $className = 'active')
{
$CI =& get_instance();
$uri_string = $CI->uri->uri_string();
// Home is usually at / && has 0 total segments
if ($path === '/' && ($CI->uri->total_segments() === 0)) {
$ret_val = 'active';
} else {
$ret_val = ($uri_string === $path) ? $className : '';
}
return $ret_val;
}
MohsinAhmedShaikh commented on Oct 18, 2017
i use peterson-umoke code and its works on these lines
<li class="<?php echo show_current_class('addmovies'); ?>"><a
href="addmovies">Add Movie</a></li>
<li class="<?php echo show_current_class('movieslist'); ?>"><a
href="movieslist">Movies List</a></li>
<li class="<?php echo show_current_class('search_movie'); ?>"><a
href="search_movie">Search Movies</a>
but could not add active class without have href following line
<a class="<?php echo show_current_class('Movies'); ?>" href="i dont have any
page for this">
please help
my sidebar menu code are
<aside>
<div id="sidebar" class="nav-collapse ">
<!-- sidebar menu start-->
<ul class="sidebar-menu" id="nav-accordion">
<li>
<a href="index.php">
<i class="icon-dashboard"></i>
<span>Dashboard</span>
</a>
</li>
<li class="sub-menu">
<a class="<?php echo show_current_class('Movies'); ?>"
href="i dont have any page for this">
<i class="icon-tasks"></i>
<span>Movies</span>
</a>
<ul class="sub">
<li class="<?php echo
show_current_class('addmovies'); ?>"><a href="addmovies">Add Movie</a></li>
<li class="<?php echo
show_current_class('movieslist'); ?>"><a href="movieslist">Movies List</a>
</li>
<li class="<?php echo
show_current_class('search_movie'); ?>"><a href="search_movie">Search
Movies</a></li>
</ul>
</li>
<li class="sub-menu">
<a href="javascript:;" class="<?php echo
show_current_class('home'); ?>">
<i class="icon-tasks"></i>
<span>Tv Shows</span>
</a>
<ul class="sub">
<li><a href="addmovies">Add Tv Show</a></li>
<li><a href="movieslist">Tv Shows List</a></li>
<li><a href="search_movie">Search Tv Shows</a>
</li>
</ul>
</li>
</ul>
<!--multi level menu end-->
<!-- sidebar menu end-->
</div>
</aside>
Thanks
chunchillo commented on Jul 19, 2018 • edited
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('activate_menu')) {
function activate_menu($link, $css = "") {
if ( isset($_SESSION["link"]) ) {
$controller = $_SESSION["link"];
} else {
$controller = "inicio";
}
return ($controller == $link) ? "{$css} active" : $css;
unset($_SESSION["link"]);
}
}
?>
In your controllers o methods put in
<?php
$this->session->set_flashdata("link", "name_of_menu_to_activate");
?>
In your menu boostrap
<li class="nav-item <?= activate_menu("name_of_menu_to_activate"); ?>">
<a class="nav-link" href="#">link</a>
</li>
a-tesh commented on Aug 23, 2019 • edited
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 commented on Feb 9, 2020 • edited
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 commented on 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 commented on Sep 10, 2020
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.