0

I am using CakePHP and I have a doubt.

  1. I have all my views in app/view/pages/
  2. I use url routing Router :: connect ('/', array ('controller' => 'I REMOVED THE WORD PAGES HERE', 'action' => 'index')); instead of
    Router :: connect ('/', array ('controller' => 'pages', 'action' => 'display', 'home'));
    This allows me to have www.site.com/contact instead of www.site.com/pages/contact
    Where I put "I REMOVED THE PAGES WORD HERE", this is only the single quotes.
  3. In my menu, I have the following HTML (app/view/layouts/default.ctp):

    href="contact">Contact 
    href="about">About
    

    Above, the aim was to show the UL LI but I do not know how to expose code here.

  4. I read over 30 different pages on the subject, but no code shown solved my problem. What I want is to highlight the active menu LI CLASS = "ACTIVE", that's all. If i'm in www.site.com/contact, my menu should highlight out that I'm on the 'contact' etc.

In PHP I can do, but in CakePHP can not.

Could someone please help me? Thank you.

This is an exemple of what i'm talking about: how to add active class in current page in CakePhp
But in my case, this is just a simples aplication. I don't use database and i don't use no any controller. I just use layout stuffs.

Community
  • 1
  • 1
Steve
  • 19
  • 1
  • 4
  • 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:57

2 Answers2

5

you can know currenty on which page you are by $this->params['action'];, so you use can this this to set active class either in <li> or in <a>

<ul id="selectnav">
                <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='home') )?'active' :'inactive' ?>">
                  <a href="/"><i class="icon-hdd"></i>Home</a>
                </li>
                <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='about') )?'active' :'inactive' ?>">
                  <a href="/aboutus"><i class="icon-heart-empty"></i>About</a>
                </li>

                <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='contact') )?'active' :'inactive' ?>">
                    <a href="/contactus"><i class="icon-envelope-alt"></i>Contact</a>
                </li>
              </ul>

OR

 <ul id="selectnav">
                    <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='home') )?'active' :'' ?>">
                      <a href="/"><i class="icon-hdd"></i>Home</a>
                    </li>
                    <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='about') )?'active' :'' ?>">
                      <a href="/aboutus"><i class="icon-heart-empty"></i>About</a>
                    </li>

                    <li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='contact') )?'active' :'' ?>">
                        <a href="/contactus"><i class="icon-envelope-alt"></i>Contact</a>
                    </li>
                  </ul>

The question you referred was right, you can use this method

karmicdice
  • 1,063
  • 9
  • 38
  • Checking only the action might mislead you as soon as you use multiple controllers with similiar-named actions. You should better check whether the normalized urls of the current request and the current request match (like in [this answer](http://stackoverflow.com/a/28616634/1062304)). – bfncs Feb 19 '15 at 20:56
  • 1
    People using cakephp 3 must use `this->request->params` instead of `this->params` – Varun Rao May 26 '15 at 07:30
0

Have you seen the MenuHelper on the Bakery? It allows you to let Cake apply those classes for you without having to worry about it:

http://bakery.cakephp.org/articles/alkemann/2009/02/04/menuhelper

If you're going to stick with the manual way, I'd suggest doing what Aryan has posted, but define your action variable first to save space and readability. In this example I've used the ternary operator:

<?php
$action = !empty($this->params['action']) ? $this->params['action'] : null;
?>
...
<li class="<?=($action == "home) ? 'active' : ''?>">...
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • The code also dont works. I tried to install this plugin, but i don't know how to. I read http://book.cakephp.org/2.0/en/views/helpers.html but its not clear enought. Also, i read http://bakery.cakephp.org/articles/alkemann/2009/02/04/menuhelper#page-1 and i don't know about the fhrase "add 'Menu' to AppController's $helpers property". I swer, I tried to install it but couldn't. – Steve Dec 01 '13 at 20:47
  • You can put it into your `$helpers` variable in your `pages` controller... at the top of your controller code, find the line like `var $helpers = array(...` and add `'Menu'` to to it: `var $helpers = array('Menu', ...the rest` – scrowler Dec 01 '13 at 22:05