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''>
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>
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.
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>";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With