V
V
Viktor Yanyshev2017-06-01 12:57:22
Yii
Viktor Yanyshev, 2017-06-01 12:57:22

How to set the active menu item class to Nav::widget?

There is a table in the database that populates the items array in Nav. Pages that have template_name specified have an Article controller and a default index method.

+----+------------------------------+-----------+---------------+---------------+
| id | title                        | type      | url           | template_name |
+----+------------------------------+-----------+---------------+---------------+
|  5 | О фабрике                    | page      | /about        | article       |
|  6 | Новинки                      | page      | /products/new | main          |
|  7 | Каталог                      | container | /catalog      | main          |
|  8 | Сотрудничество               | page      | /partners     | article       |
|  9 | Мастерская                   | page      | /master_class | main          |
| 10 | Новости                      | container | /news         | main          |
| 11 | Контакты                     | page      | /contacts     | main          |
+----+------------------------------+-----------+---------------+---------------+

In UrlManager specified rules for opening pages under the specified url in the database
'rules' => [
                '/about' => 'article/index',
                '/partners' => 'article/index',
            ],

But due to this approach (bicycle?), Nav doesn't set the class of the active menu item as the urls don't look like "Controller/Method" but I need different urls that are manually set (assuming manual setting) in the app's backend.
How to solve setting the class of the active menu item, in this situation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-06-01
@villiwalla

Your problem is related to the fact that by default the Nav widget takes the current route from the controller that is called and without considering the url rules. You can solve this problem by setting the route for Nav yourself, like this:

<?= Nav::widget([
    'route' => Yii::$app->getRequest()->pathInfo,
    'items' => $items
]);

M
Maxim Timofeev, 2017-06-01
@webinar

I think at least it should be like this:

'rules' => [
                '/<url>' => 'article/index',
            ],

and action
public function actionIndex($url){
   return $this->render('some_view',[
      'model' => SomeModel::findOne(['url'=>$url]);
   ]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question