Widget CMenu Problem?

Hello!

This is my URL Manager:




'urlManager' => array(

	'urlFormat' => 'path',

	'showScriptName' => false,

	'caseSensitive' => true,

	'rules' => array(

		'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

		'<language:\w+>/' => 'site/index',

		'<language:\w+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',

	),

),



This is my CMenu:




<?php 


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

        'linkLabelWrapper' => 'div',

        'lastItemCssClass' => 'lastMenuOption',

        'items' => array(

                array('label' => Yii::t('pages', 'home'), 'url' => array('/'.Yii::app()->language.'/site/index')),

                array('label' => Yii::t('pages', 'singUp'), 'url' => array('/'.Yii::app()->language.'/users/singUp'), 'visible' => Yii::app()->user->isGuest),

                array('label' => Yii::t('pages', 'ranking'), 'url' => array('/'.Yii::app()->language.'/users/ranking')),

                array('label' => Yii::t('pages', 'about'), 'url' => array('/'.Yii::app()->language.'/site/about')),

                array('label' => Yii::t('pages', 'contact'), 'url' => array('/'.Yii::app()->language.'/site/contact')),


                array('label' => Yii::t('pages', 'error'), 'url' => array('/'.Yii::app()->language.'/site/error'), 'visible' => $this->route === 'site/error'),

        ),

));



Obviously, the items were not with the active class.

Is there any way to solve this?

If I put the url attribute ‘url’ => array(’/site/index’, array(‘language’ => Yii::app()->language) works, but, how not to lose the friendly url?

I solved the problem:


<?php 


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

	'linkLabelWrapper' => 'div',

	'lastItemCssClass' => 'lastMenuOption',

	'items' => array(

		array('label' => Yii::t('pages', 'home'), 'url' => array('/'.Yii::app()->language.'/site/index'), 'itemOptions' => array('class' => $this->route === 'site/index' ? 'active' : '')),

		array('label' => Yii::t('pages', 'singUp'), 'url' => array('/'.Yii::app()->language.'/users/singUp'), 'visible' => Yii::app()->user->isGuest, 'itemOptions' => array('class' => $this->route === 'users/singUp' ? 'active' : '')),

		array('label' => Yii::t('pages', 'ranking'), 'url' => array('/'.Yii::app()->language.'/users/ranking'), 'itemOptions' => array('class' => $this->route === 'users/ranking' ? 'active' : '')),

		array('label' => Yii::t('pages', 'about'), 'url' => array('/'.Yii::app()->language.'/site/about'), 'itemOptions' => array('class' => $this->route === 'site/about' ? 'active' : '')),

		array('label' => Yii::t('pages', 'contact'), 'url' => array('/'.Yii::app()->language.'/site/contact'), 'itemOptions' => array('class' => $this->route === 'site/contact' ? 'active' : '')),


		array('label' => Yii::t('pages', 'error'), 'url' => array('/'.Yii::app()->language.'/site/error'), 'visible' => $this->route === 'site/error', 'itemOptions' => array('class' => $this->route === 'site/error' ? 'active' : '')),

	),

));

But it was not intuitive.

Does anyone know a better way to solve it?

Hi,

try to pass the language key as a parameter as follows:




...

'items' => array(

	array('label' => Yii::t('pages', 'home'), 'url' => array('site/index', 'language'=>Yii::app()->language)),

	array('label' => Yii::t('pages', 'singUp'), 'url' => array('users/singUp', 'language'=>Yii::app()->language), 'visible' => Yii::app()->user->isGuest),

	...



To understand why the above code works try to read CHtml::normalizeUrl method documentation and remember that UrlManager is a two way facility:

[list=1][]it can parse a speaking URL into the route and GET paramters[]it can construct speaking URL by a given route and GET paramters that are defined in rules (the language parmater in our case)[/list]

MadAnd, thanks for the contribution.

The problem of the model that you presented, is that I miss the friendly url.

instead of being .../<language>/<controller>/<action>, will be .../<controller>/<action>?language=<language>

Ah sorry,

i’ve just forgot to note that you also need to adjust the rules, since their declaration order is also important:




'urlManager' => array(

        'urlFormat' => 'path',

        'showScriptName' => false,

        'caseSensitive' => true,

        'rules' => array(

                '<language:\w+>/' => 'site/index',

                '<language:\w+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',

                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

        ),

),



Now my previous example should work the way you need.