U4EA
November 23, 2012, 12:05am
1
Hi folks,
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…
'url'=>array('/site/index')
'url'=>array('/site/about')
to this…
'url'=>array('/index')
'url'=>array('/about')
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.
yugenekr
(Yugenekr)
November 23, 2012, 5:59am
2
Hi U4EA,
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.
Example:
array('label' => 'Settings', 'url' => array('/admin/settings'), 'active' => ($this->id === 'settings')),
U4EA
November 24, 2012, 2:15pm
4
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?
seenivasan
(Chellamnivas)
November 24, 2012, 3:51pm
5
Dear U4EA
I hope the following is helpful in this scenario.
array('label'=>$this->id==="site"?"Home":"List", 'url'=>array('index'),'active'=>($this->action->id==="index")),
Regards.
U4EA
November 24, 2012, 3:53pm
6
Works perfectly. Thank you!
U4EA
November 24, 2012, 4:04pm
7
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?
konapaz
(Konapaz)
November 24, 2012, 4:15pm
8
in your view files add this code
echo $this->action->id
tell us what is the output in both cases
seenivasan
(Chellamnivas)
November 24, 2012, 4:23pm
9
Dear U4EA
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.
Regards.
U4EA
November 25, 2012, 12:55pm
10
For the root (www.website.com/ ) and the other classes, it is ‘index’.
Calling
echo $this->action->id;
produces ‘site’ for the root, then the expected ‘index’ for the home page, ‘about’ for about etc.
U4EA
November 25, 2012, 1:02pm
11
Hi folks,
I modified it to the following, which seems to be working fine…
'active' => ($this->id==="site" || $this->id==="index")
Thanks for the help guys!
U4EA
November 29, 2012, 5:50pm
12
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…
'defaultController'=>'index',
Works for for me and I can’t see any drawbacks to it?