Menu - condition


I wanted to put visible or not visible menu items depending on the page that is on site.

Through the,

array(‘label’=>‘Home’, ‘url’=>array(’/site/index’), ‘visible’=>’…’),

I do not know the condition that I just put up the item home for example, if is on the home page.

I hope I have done to understand.

Thanks in advance.


In any view:

  • [font="Courier New"]$this->getId()[/font] gives you the controller name and [font="Courier New"]Yii::app()->defaultController[/font] the default controller name ("site" in the default setting)

  • [font="Courier New"]$this->getAction()->getId()[/font] gives you the action name and [font="Courier New"]$this->defaultAction[/font] the default action name of the current controller ("index" in the default setting)

So from here, you can compare both couples like this:

'visible'=>'(strcasecmp(basename($this->getId()), Yii::app()->defaultController) || strcasecmp($this->getAction()->getId(),$this->defaultAction))' /* returns false if home page ie "site/index" */

Credit: heavily inspired from this post when building a multilingual webapp

Thank you,

Works as I wanted. One more question, can I call functions in the visible for me to return true or false.

It is that if I put the conditions for all the controllers can appear where the menu items the expression of the item is too large. How can work like this:

array(‘label’=>‘Home’, ‘url’=>array(’/site/index’), ‘visible’=> isSiteOrUser())

function isSiteOrUser(){

            if(strcasecmp(basename($this->getId()), Yii::app()->defaultController) ||                                 strcasecmp(basename($this->getId()), 'user')){

                return true;

            }else return false;


I’m doing this in the main menu. I put this function in a different class?if it is possible.

Thank you bennouna

You can put it in protected/layouts/main.php, or if you use that condition across other views, I guess you can put it in protected/components/Controller.php (it’s an extension of CController which all your webapp controllers extend from, in a standard configuration).

As a follow up to this concept, I wanted to have my menu’s visible but disabled. So the current page (be it admin, update, view…) is always disabled in the menu but the menu stays looking the same. ( I had a user request this…personally I don’t agree but trying to provide what is asked)

Thanks for any advice.

below is my code


array('label'=>Yii::t('app', 'My Account') , 'url'=>array('console', 'id'=>$model->id)),

array('label'=>Yii::t('app', 'Update Account') , 'url'=>array('update', 'id'=>$model->id)),

array('label'=>Yii::t('app', 'Approve') , 'url'=>array('approve', 'id'=>$model->id)),

array('label'=>Yii::t('app', 'Denied') , 'url'=>array('denied', 'id'=>$model->id), [b]array('disabled'=>'disabled')[/b]),


Dear Friend

Kindly check whether the following would be helpful in your scenario.


array('label'=>Yii::t('app', 'Update Account') , 'url'=>(!Yii::app()->user->isGuest)?array('update', 'id'=>$model->id):""),

array('label'=>Yii::t('app', 'Approve') , 'url'=>(Yii::app()->user->id==1)?array('approve', 'id'=>$model->id):""),