这是我的代码:
<?=
Nav::widget([
'options' => ['class' =>'nav-pills nav-stacked'],
'encodeLabels' => false,
'items' => [
[
'label' => 'Blog',
'url' => ['/blog'],
'active' => \Yii::$app->controller->id == 'blog'
],
],
]);
?>
但是我希望这个菜单不仅在控制器是&#34;博客&#34;时也是活跃的,而且当它是&#34;类别&#34;和&#34;发布&#34;。
答案 0 :(得分:6)
更改您的代码,如下所示:
Nav::widget([
'options' => ['class' =>'nav-pills nav-stacked'],
'encodeLabels' => false,
'items' => [
[
'label' => 'Blog',
'url' => ['/blog'],
'active' => in_array(\Yii::$app->controller->id,['blog','category','post'])
],
],
]);
通过上面的代码,如果控制器ID是['blog','category','post']
值之一,则您的菜单项将被视为活动状态。唯一的变化是:
'active' => in_array(\Yii::$app->controller->id,['blog','category','post'])