I have started to develop a website using the default Yii webapp as the template.
It’s all going fine but I have come to this problem…
I tried to clean up the URLs and have done so successfully. However, when I change the CMenu URL link from this format…
Everything works fine except the
active class is not assigned. I should probably mention here that I have moved away from SiteContoller to individual controllers for the index, about, contact etc.
Anything I can do to rememdy this?
Thanks in advance.
In a case page URL isn’t the same as CMenu link URL (like in your case), you may reform link URL or use property ‘active’.
As it is said in CMenu documentation,
active: boolean, optional, whether this menu item is in active state (currently selected). If a menu item is active and activeClass is not empty, its CSS class will be appended with activeClass. If this option is not set, the menu item will be set active automatically when the current request is triggered by url. Note that the GET parameters not specified in the ‘url’ option will be ignored.
array('label' => 'Settings', 'url' => array('/admin/settings'), 'active' => ($this->id === 'settings')),
One thing that kinda leads on from this…
If I do not specifiy a page i.e. I just use the URL route of
www.thesite.com/, it does not assign the class to the ‘index’ menu item. Is there a way I can fix this to use route incoming requests with no specified controller to the index controller?
I hope the following is helpful in this scenario.
Works perfectly. Thank you!
Just noticed an issue with that. When I go to
www.website.com/about then it sets both the url=>index and url=>about to active. Any ideas?
in your view files add this code
tell us what is the output in both cases
This is what document on CMenu says.
What we are doing is something tricky.
In my suggestion, obviously we have to ensure that we should not have two items with same action(index).
Someone may suggest a better workaround.
For the root (
www.website.com/) and the other classes, it is ‘index’.
produces ‘site’ for the root, then the expected ‘index’ for the home page, ‘about’ for about etc.
I modified it to the following, which seems to be working fine…
'active' => ($this->id==="site" || $this->id==="index")
Thanks for the help guys!
Just as an after thought, I would imagine it would also be a perfectly acceptable solution to configure the
front loader to have ‘index’ as the default controller by adding this to $config…
Works for for me and I can’t see any drawbacks to it?