CMenu and Active menu items

This is mine JQuery solution which works fine for mine dropdown menu with actions from different controllers.

Perhaps it’ll be useful for somebody.




<script type="text/javascript">

            jQuery(function($) {

                $('a[href=\'<?php echo Yii::app()->request->requestUri; ?>\']').parents('li').addClass('active');

            });

</script>



Here is my code,

can’t highlight menu item as

<?php $this->widget(‘zii.widgets.CMenu’,array(

		'items'=&gt;array(


			array('label'=&gt;'Home', 'url'=&gt;array('/site/index')),


			array('label'=&gt;'Project', 'url'=&gt;array('/project')),


			array('label'=&gt;'Issue', 'url'=&gt;array('/issue')),


			array('label'=&gt;'User', 'url'=&gt;array('/user')),


			array('label'=&gt;'About', 'url'=&gt;array('/site/page', 'view'=&gt;'about')),


			array('label'=&gt;'Contact', 'url'=&gt;array('/site/contact')),


			array('label'=&gt;'Login', 'url'=&gt;array('/site/login'), 'visible'=&gt;Yii::app()-&gt;user-&gt;isGuest),


			array('label'=&gt;'Logout ('.Yii::app()-&gt;user-&gt;name.')', 'url'=&gt;array('/site/logout'), 'visible'=&gt;&#33;Yii::app()-&gt;user-&gt;isGuest)


		),


	)); ?&gt;

following code can highlight menu item automatically, after add the "/index"

<?php $this->widget(‘zii.widgets.CMenu’,array(

		'items'=&gt;array(


			array('label'=&gt;'Home', 'url'=&gt;array('/site/index')),


			array('label'=&gt;'Project', 'url'=&gt;array('/project/index')),


			array('label'=&gt;'Issue', 'url'=&gt;array('/issue/index')),


			array('label'=&gt;'User', 'url'=&gt;array('/user/index')),


			array('label'=&gt;'About', 'url'=&gt;array('/site/page', 'view'=&gt;'about')),


			array('label'=&gt;'Contact', 'url'=&gt;array('/site/contact')),


			array('label'=&gt;'Login', 'url'=&gt;array('/site/login'), 'visible'=&gt;Yii::app()-&gt;user-&gt;isGuest),


			array('label'=&gt;'Logout ('.Yii::app()-&gt;user-&gt;name.')', 'url'=&gt;array('/site/logout'), 'visible'=&gt;&#33;Yii::app()-&gt;user-&gt;isGuest)


		),


	)); ?&gt;

'active'=>$this->id=='item'?true:false

This worked for me.

Does anyone know how I would keep this menu item active for multiple controller? I have tried many combinations to try and make it work for the item and profile and rights controller but no luck… any suggestions?

This worked perfectly for me and only a 1 line solution. Nice.

as told ‘Backslider’, this works…


array('label'=>'Invoice Client', 'url'=>array('invoice/client'), 'visible'=>!Yii::app()->user->isGuest, 'active'=>$this->action->id=='client'),


'active'=>$this->id=='item'?true:false

A shorter version will also work:


'active'=>$this->id=='item'

And for those of you working with modules, this is what I’m using:


array('label'=>'Admin', 'url'=>array('/admin'), 'active'=>!is_null($this->module) ? $this->module->id=='admin' : false)

Works for all the controllers inside the ‘admin’ module.

I thought I’d share this hoping it helps someone.

Nice solution.

Thanks. This worked for me !!

‘active’=>Yii::app()->controller->id == ‘sys001’,

– without the <controller> –

This is my solution for module




array('label'=>'Biblioteka', 'url'=>array('/library'), 'active'=>(is_object($this->module)&&$this->module->id==='library')?true:false),