简体   繁体   中英

Yii CMenu active item not being set

I'm using Yii's CMenu to generate menu in the following way:

$this->widget('zii.widgets.CMenu', array(
        'items'=>$this->mainMenu,
        'lastItemCssClass'=>'mitem-last',
        'activeCssClass'=>'mitem-active',
        'activateParents'=>true
));

I am displaying this menu on page index.php?r=site/home and was expecting the following menu item to have mitem-active class:

[2] => Array
(
    [label] => test
    [url] => /index.php?r=site/home
)

However the item is rendered as

<li><a href="/index.php?r=site/home">test</a></li>

If I'm not mistaken index.php?r=site/home has route site/home (which is also the output of $this->route ), and as a matter of fact the link was created using

$link['url']=$this->createUrl(current($mitem['url']),$params);

where current($mitem['url']) outputs site/home and $params is just array() .

Am I missing something?

EDIT: $this->mainMenu is an array made up off multiple $link s built in a loop (from xml).

$this->mainMenu = array();
foreach($xml->mitem as $mitem){
    $link=array();

    ... compute some values like $link['label'], $params etc ...
    $link['url']=$this->createUrl(current($mitem['url']),$params);
    $this->mainMenu[]=$link;
}

For CMenu, "items" array format is:

array(
    array('label'=>'Home', 'url'=>array('site/index')),
    array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
    array('label'=>'Contact', 'url'=>array('/site/contact')),
)

not

array(
    array('label'=>'Home', 'url'=>'index.php?r=site/index'),
    array('label'=>'About', 'url'=>'index.php?r=site/page&page=about'),
    array('label'=>'Contact', 'url'=>'index.php?r=site/contact'),
)

If your item looks like this:

array('label'=>'Home', 'url'=>'index.php?r=site/home'),

It does not work. Your item must be like this:

array('label'=>'Home', 'url'=>array('site/home')),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM