There seems to be a few minor quirks with the CMenu and clean URLs.
If I have a url as array(’/site/page’, ‘view’=>‘about’)
The menu will not use the clean url: /site/page/view/about Instead it will use: /site/page?view=about
I assumed this to be an issue with normalizeUrl, so I tried to use the clean URL instead.
I set the url to "/site/page/view/about"
The menu works fine and the page loads correctly, but it does not properly identify the active state of the current page within the menu.
Am I missing something?
perochak
(Amjad Mughal)
May 19, 2011, 5:51pm
2
Check your URLManager settings in protected/config/main.php
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
perochak
(Amjad Mughal)
May 19, 2011, 6:08pm
4
So, you need to set them as you can see that
in the url manager, you have not define rule for pages
To get
('/site/page', 'view'=>'about')
as
/site/page/view/about
You need to create URL as
'/site/page/<page_name:.*?>'=>'site/page',
Where page_name is the value for $_GET[‘page_name’] or $_POST[‘page_name’]
PeRoChAk:
You need to create URL as
'/site/page/<page_name:.*?>'=>'site/page',
Where page_name is the value for $_GET[‘page_name’] or $_POST[‘page_name’]
What about view (the action)?
Also, I do not understand why I need to add additional rules if /site/page/view/about works as a URL. Again, I can navigate to this page, it renders the proper content, but the menu does not show it as active.
perochak
(Amjad Mughal)
May 19, 2011, 6:36pm
6
Ops
Then you need to set
'activateItems'=>true,
'activateParents'=>true,
In CMenu Property
perochak
(Amjad Mughal)
May 19, 2011, 6:37pm
7
Here is example
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'activateItems'=>true,
'activateParents'=>true,
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'Groups', 'url'=>array('/social/groups')),
array('label'=>'Blogs', 'url'=>array('/social/blogs')),
array('label'=>'Photos', 'url'=>array('/social/photos')),
array('label'=>'Videos', 'url'=>array('/social/videos')),
array('label'=>'TV & Radio', 'url'=>array('/media')),
),
)); ?>
</div>
Here’s my updated config:
'gii'=>'gii',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<view:(program|get|sponsor|gallery|news)>'=>'site/page',
My update menu:
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Program<br />Information', 'url'=>'/program'),
array('label'=>'Get A<br />Monkey', 'url'=>'/get'),
array('label'=>'Sponsor<br />A Monkey', 'url'=>'/sponsor'),
array('label'=>'Monkey<br />Gallery', 'url'=>'/gallery'),
array('label'=>'Monkey<br />News', 'url'=>'/news'),
),
'lastItemCssClass'=>'last',
'encodeLabel'=>false,
'activateItems'=>true,
'activateParents'=>true,
)); ?>
I access a page by <domain>/sponsor but sponsor is not active in the menu. Same issue.
perochak
(Amjad Mughal)
May 19, 2011, 7:00pm
9
give your HTML output of menu
perochak
(Amjad Mughal)
May 19, 2011, 7:10pm
10
This will help you
http://www.yiiframework.com/forum/index.php?/topic/13795-cmenu-unable-to-set-parent-menu-item-active/
array('label'=>'Sponsor<br />A Monkey', 'url'=>'/sponsor','active'=>(($this->id=='get')||($this->action->id=='program'))),
Yes, I was hoping that CMenu would still manage the active state automatically, but I guess not.
I was temporarily doing ‘active’=>($_GET[‘view’] == ‘sponsor’ ? true:false) but your solution is cleaner, thanks.