tuga
(Tuga74)
March 24, 2010, 6:11pm
1
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
samdark
(Alexander Makarov)
March 24, 2010, 7:17pm
2
It should match this way.
tuga
(Tuga74)
March 25, 2010, 12:54am
3
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
samdark
(Alexander Makarov)
March 25, 2010, 1:56am
4
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 .
jerry2801
(Jerry2801)
March 25, 2010, 2:44am
5
array(
array(
'label' => 'SERVICES ',
'url' => array('/services/index'),
),
)
tuga
(Tuga74)
March 25, 2010, 9:11am
6
samdark:
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 .
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.
zaccaria
(Matteo Falsitta)
March 25, 2010, 9:58am
7
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
tuga
(Tuga74)
March 25, 2010, 11:56am
8
zaccaria:
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.