Bart Platak
Bart Platak

Reputation: 4475

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 $links 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;
}

Upvotes: 0

Views: 4403

Answers (1)

Anton Kucherov
Anton Kucherov

Reputation: 307

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')),

Upvotes: 3

Related Questions