Dynimic Menu

How to create a dynamic menu and submenu from database in /view/layouts/main.php?

I want make some queries and show the items in my menu.

The Bootstrap Nav control is configured (like most widgets) from various arrays. You can create these dynamically in the controller action and then pass them to the view like the other objects so that the Nav can access the variable instead of hard-coded arrays.

The problem is how to pass it before any controller? The file /view/layouts/main.php is not loaded by controllers like SiteController or XptoController. It is loaded before.

I usually create custom widgets for dynamic menus. The widget class is responsible for both fetching menu data from db and generating all HTML.

Add it to view params - like it is done for the breadcrumbs.

Anything in Yii::$app->view->params is available not only in your views but also in the layouts.

For example:

Yii::$app->view->params['sidemenu'] = $sidemenu_array;