M
M
Maxim2015-02-12 14:35:32
Yii
Maxim, 2015-02-12 14:35:32

How to properly set active item in Yii2 menu?

The essence is this, there is a menu, in it the url of links is formed by the line Url::current(['cost' => $i]), example:

echo Menu::widget([
    'items' => [
['label' => $i, 'url' => Url::current(['cost' => $i])],
],
    'options' => [
        'class' => 'list-inline',
    ],
]);

Due to the fact that instead of the address ['controller/action', 'param1'=>'val1'] there is a string with the current parameters (necessary for filtering by content), the menu widget does not substitute the css class 'active' for active items. At the moment the solution is:
['label' => $i, 'url' => Url::current(['cost' => $i]), 'active' => Yii::$app->request->get('cost') == $i];

But I would like to have this functionality automatically, is there a better solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Makarov, 2015-02-17
@go3l337

Not out of the box, but you can always do app\widgets\Menu extends yii\widgets\Menu.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question