I
I
Igor Vasiliev2017-05-10 11:49:21
MySQL
Igor Vasiliev, 2017-05-10 11:49:21

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();
    ?>

What's wrong here? You ask, yes, it’s very simple, you need to replace it with
$_SERVER['REQUEST_URI']
a Yii2 variable, which shows the entire WEB path, not the directory from the server, I’m talking about the code . At least if you use it on OpenServer, you can see the entire path starting from the disk on which it is installed . I need 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. Second. Security. I'm just ready to beat my fingers for this, but have not yet figured out how to withdraw
<?=Yii::getAlias('@webroot', $model->id);?>
<?php
$link = Menu::find()->orderBy(['sort' => SORT_ASC])->limit(10)->all();
?>

in the controller so that it works in the "layout" on all pages, so that each time you do not write this line in the action (well, you understand what I'm talking about).
Third. Multilevel menu.
--
Let's just say that there is a certain column in the database table, let's call it url, and it will be responsible for levels, roughly speaking, if the link has url=12, then all submenus will be placed on the site where this level is specified.
For those who do not understand, here is an example of work, architecture and DOM tree of the menu.
www.magisters.org/community/3/17
Bootstrap 4 may be different, haven't looked yet. But in the example at the link, you can see that there are 3 levels, I think it’s not bad, and it’s quite suitable for standard solutions. Conditions with active link highlighting, I think you can do the same.
--
Thanks in advance for your input on this issue. The young man is waiting for wise advice on the true path.
May the force be with us)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-05-10
@qonand

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.

Why not just use the default property of the Nav activateItems class then ?
Where is the security here? Yes, you should not access the database in the layer, but this does not affect security.
In order not to access the database directly in the layer, implement a widget that will do this.
what is the actual question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question