how to add active class in current page in CakePhp
Asked Answered
L

3

6

i have a problem similar to this question

How to identify active menu link in CakePHP

i have a page in my default.ctp file in which i want to add 'active' class on links. how can i identify the current url of the page and then apply the class on link.. i have followed the answer also there which is

      $url = $this->Html->url('INPUT_THE_URL') ;
     $active = $this->request->here == $url? true: false;

i dont know how can i do this in my code .. sorry for asking as i am newbie in cakephp .. here is my code

 **default.ctp file** 

 <li>
      <?php echo $this->Html->link('Dashboard', array('controller'=>'users','action' => 'controlpanel'), array('title' => 'Dashboard','class' => 'shortcut-dashboard'));?></li>



  <li> <?php echo $this->Html->link('Contacts', array('controller'=>'contacts','action' => 'index'), array('title' => 'Contacts','class' => 'shortcut-contacts'));?></li>

i want to add a class with li like this

   <li class = 'active''>
Lanner answered 18/6, 2013 at 6:31 Comment(1)
possible duplicate of How to identify active menu link in CakePHPBenham
K
12

This is a simple logic as follows

<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='controlpanel') )?'active' :'inactive' ?>">
  <?php echo $this->Html->link('Dashboard', array('controller'=>'users','action' => 'controlpanel'), array('title' => 'Dashboard','class' => 'shortcut-dashboard'));?>
</li>

<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='index') )?'active' :'inactive' ?>">
  <?php echo $this->Html->link('Contacts', array('controller'=>'contacts','action' => 'index'), array('title' => 'Contacts','class' => 'shortcut-contacts'));?></li>
Keare answered 18/6, 2013 at 7:7 Comment(1)
thankkkkkkkkkkkkkkkkkkkkk you it works really ...thankuu budyy .god bless youLanner
Q
8

If you have a different controller and you have declared a method with same name, and the above code is not working, then you can do the following:

<li class="<?php echo (($this->params['controller']==='hotels')&& ($this->params['action']=='view') )?'active' :'' ?>" >
   <?php echo $this->Html->link('Hotels', array('controller' => 'hotels', 'action' => 'view')); ?>
</li>

<li class="<?php echo (($this->params['controller']==='packages')&& ($this->params['action']=='view') )?'active' :'' ?>" >
   <?php echo $this->Html->link('Packages', array('controller' => 'packages', 'action' => 'view')); ?>
</li>

Here view method is declared in different controller. i hope it will be helpful for you.

Quaint answered 16/5, 2014 at 5:47 Comment(0)
R
1

Not to revive a dead post, but this is what I do (which I believe is a bit cleaner and faster and a bit more manageable)

I create an element that has an array of pages, then I check against each item in the array to see if it is the current page. If it is I add the active class.

I can then call this element from anywhere.

// Changed the line below to a multi-dimensional array to cater for different controllers and actions

//$mypages = array('Home','About','Pricing','FAQs','Contact');
$mypages = array(
 array('controller'=>'controller1','action'=>'action1','name'=>'name1'),
 array('controller'=>'controller2','action'=>'action2','name'=>'name2
')
);
foreach ($mypages as $page ){
// Changed to account for controller and action
//$currentPage = isset($this->params['pass'][0]) ?$this->params['pass'][0] : "";
$controller = isset($this->request->params['controller'])?$this->request->params['controller']: "";
$action= isset($this->request->params['action'])?$this->request->params['action']: "";

    if (strtolower($page['controller']) == $controller && strtolower($page['action']) == $action) {  
        echo "<li class='active'>" . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page))) . "</li>" ;  
    } 
    else  {
        echo "<li>" . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page)))  . "</li>"; 
    }
}
Remediless answered 14/12, 2015 at 11:27 Comment(2)
I'm searching for a cleaner way to to this like your example. But how would you do it if you have different controllers and all of them have actions with the same name?Cudlip
@Cudlip I guess it depends on how many pages you have. I only have a few. But if you have different controllers and actions, you could probably make it a multi-dimensional array. I will update my answer to show.Remediless

© 2022 - 2024 — McMap. All rights reserved.