How to add an active class on the current page in CakePhp

I have a problem similar to this question

How to identify an active menu link in CakePHP

I have a page in the default.ctp file in which I want to add the "active" class in the links. how can I identify the current URL of the page and then apply the class by reference .. I followed the answer and where there is

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

I don't know how I can do this in my code .. sorry for the question, how am I new to 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

  <li class = 'active''> 
+6
source share
3 answers

This is simple logic:

 <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> 
+12
source

If you have another controller, and you declared a method with the same name, and the above code does not work, 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, the view method is declared in another controller. I hope this will be helpful to you.

+7
source

Do not resurrect dead mail, but this is what I do (which, in my opinion, is a little cleaner and faster and a little more manageable)

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

Then I can call this item 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>"; } } 
+1
source

Source: https://habr.com/ru/post/947523/


All Articles