zii.widgets.CMenu is controller dependent?

I have two controllers, [font="Courier New"]SiteController[/font] and [font="Courier New"]UserController[/font] (from the Yii User extension). Both controllers render views that end up with the [font="Courier New"]main.php[/font] layout file. In [font="Courier New"]main.php[/font], I create a menu with:


 $this->widget('zii.widgets.CMenu',array(

				'items'=>array(

					array('label'=>'media archive', 'url'=>array('site/archive')),

                                        array('label'=>'upload photos, movies', 'url'=>array('/site/upload')),

                                        array('label'=>'create new album', 'url'=>array('/site/calbum')),

				),

			)); 

This is all completely standard stuff. From the [font="Courier New"]SiteController[/font], the CMenu widget dutifully creates the following HTML:

[html]<li><a href="/site/archive">media archive</a></li>

<li><a href="/site/upload">upload photos, movies</a></li>

<li><a href="/site/calbum">create new album</a></li>[/html]

And all is well…

However, from my [font="Courier New"]UserController[/font], the same CMenu widget creates the following menu

[html]<li><a href="/user/site/archive">media archive</a></li>

<li><a href="/site/upload">upload photos, movies</a></li>

<li><a href="/site/calbum">create new album</a></li>[/html]

As you can see, now the [font="Courier New"]site/archive[/font] has been prepended by [font="Courier New"][color="#FF0000"]user[/color][/font]. No doubt, this is related to [font="Courier New"]$this[/font] pointing to an instance of [font="Courier New"]UserController[/font] (instead of [font="Courier New"]SiteController[/font]). This issue is not hard to work around, but I would prefer to get it right.

So, I have two questions:

  1. How do you fix this? How do you prevent the [font="Courier New"]user[/font] being prepended to the path in the UserController?

  2. Why does it happen only for the [font="Courier New"]site/archive[/font] menu item? Why does it not happen for the [font="Courier New"]site/upload[/font] or [font="Courier New"]site/calbum[/font] menu items? After all, the code is identical…

Prefix all routes with a slash:




'items' => array(

  array('label' => 'media archive',  'url' => array('/site/archive')),

  //...

),



See CController::createUrl() documentation for details.

Thanks Phtamas!

I checked your solution and it works beautifully!

Much appreciated…