Search code examples
phpcsscakephpcakephp-2.0cakephp-2.1

how to add active class in current page in CakePhp


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''>

Solution

  • 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>