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''>
Community
  • 1
  • 1
hellosheikh
  • 2,929
  • 8
  • 49
  • 115
  • possible duplicate of [How to identify active menu link in CakePHP](http://stackoverflow.com/questions/11717422/how-to-identify-active-menu-link-in-cakephp) – bfncs Feb 19 '15 at 20:54

3 Answers3

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>
AnNaMaLaI
  • 4,064
  • 11
  • 53
  • 93
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.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
Faisal
  • 4,591
  • 3
  • 40
  • 49
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>"; 
    }
}
TemiGiwa
  • 411
  • 1
  • 4
  • 15
  • 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? – eve Jan 23 '16 at 17:39
  • @eve 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. – TemiGiwa Jan 26 '16 at 07:45