Yii2 Menu Widget

How do we keep the menu item active if we are on the same controller?

I have a simple menu with each item pointing to the ‘index’ of it’s controller, however I would like the menu item to remain active when using other actions of the same controller.

I do not wish to have the other actions in the menu.

I would expect the following to work, however it does not:




    ['label' => 'Categories', 'url' => ['/category']],



This will only highlight when the action is ‘index’.

You can use \Yii::$app->controller->id for active controller name in layout or in view and use \Yii::$app->controller->action->id for active action




['label' => 'Categories', 'url' => ['/category'], 'active'=> \Yii::$app->controller->id == 'category'],