Answer the question
In order to leave comments, you need to log in
Yii2+menu, how to correctly set link highlighting and display a dropdown menu?
Hello.
--
I decided to use the standard menu model in Yii2, and stuffed the code there that displays the menu items from the database. I thought that it is more convenient to manage the menu this way, you don’t have to go into the code to manage sections and subsections. Here's what I got:
<?php
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
?>
<?php
NavBar::begin([
'brandLabel' => '<img src="/img/logo_black.png" alt="Логотип"/>',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar navbar-default navbar-fixed-top',
],
]);
foreach ($link as $l){
($_SERVER['REQUEST_URI'] == $l->href) ? $menuItems[] = ['label' => $l->name, 'url' => [''], 'options' => ['class' => 'active']] : $menuItems[] = ['label' => $l->name, 'url' => [$l->href]];
}
if (!Yii::$app->user->isGuest) {
$menuItems[] = ['label' => '<i class="ionicons ion-android-create"></i>', 'url' => ['/admin']];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
$_SERVER['REQUEST_URI']
'@webroot', $model->id);
=Yii::getAlias(<?php
$link = Menu::find()->orderBy(['sort' => SORT_ASC])->limit(10)->all();
?>
Answer the question
In order to leave comments, you need to log in
I need https://toster.ru/# like this: If the current address of the browser matches, then the class is "active", if it does not match, then this class does not exist.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question