CMenu

Hi all,

I’m using CMenu widget on my layout, and I need to set the active property manually.

I’ve changed the urls with urlmanager so it will not match and will not be active.

For instance my url is http://rochat.lan/faro/airport/transfer/Praia+da+Rocha/5 but my menu item is


array(

								'label' => 'SERVICES ', 

								'url' => array(

									'/services/index')

								), 

So I thinks this will never match, is there any way of setting manually the active property??

Thanks

It should match this way.

Hi samdark,

But I could not understand, why you say it should match.

My url is h**p://rochat.lan/faro/airport/transfer/Praia+da+Rocha/5 and my item in the CMenu widget is


'url'=>array('services/index')

Could you please be more specific

Thanks

If h**p://rochat.lan/faro/airport/transfer/Praia+da+Rocha/5 actually leads to ‘services/index’ route, it will match.

If you want to do it manually:




array(

  'label' => 'SERVICES ', 

  'url' => array('/services/index'),

  'active' => true,

)



Another option is to implement your own logic by overriding http://www.yiiframework.com/doc/api/CMenu#isItemActive-detail.


array(

    array(

        'label' => 'SERVICES ', 

        'url' => array('/services/index'),

    ),

)

Thank you samdark, you were right actually one didn’t lead to the other (my mistake). The controllers are actually different.

But I think I cannot do it manually as the widget is on the layout, so the active property has to change, I cannot set it to true, because will be always active.

I think I don’t understand quite well the CMenu class.

I did find a solution, maybe not the correct but it works:

In the controller I created a property and then on the action I will tell which menu item I should highlight




class MyController extends Controller{

protected $menuActive;


public function actionIndex(){

$this->menuActive = '/services/index';

} 

}



Then on the layout I did




$this->widget('application.components.CMenu', array(

						'id' => 'mainmenu', 

						'items' => array(

							

							array(

								'label' => 'ABOUT US ', 

								'url' => array(

									'/site/about')), 

							array(

								'label' => 'SERVICES ', 

								'url' => array(

									'/services/index'), 

								'active' => $this->menuActive == '/services/index' ? true : null

								), 

							array(

								'label' => 'RESERVATIONS ', 

								'url' => array(

									'/site/reservations')), 

							array(

								'label' => 'CONTACTS', 

								'url' => array(

									'/site/contacts')))));



Thank you all for your help.

tuga, your solution works, but maybe you can try even like that:




'active' => (($this->id=='services') && ($this->action->id=='index'))




In the view template the $this is always the controller, and so in id and action you have teh current controller name and action.

That’s allows you to avoid you to extend controller, and you can put all active checks of menu in the menu itself

Thank you zaccaria for your help, but that actually wouldn’t work as the controller is another. In your solution even normal match would occur.

Thanks anyway.