Unfortunately, yii\widgets\Menu does not support dynamic menu (having menu items stored in a database).
In a portal I previously developed, and which was not MVC/framework based (procedurally programmed), it was fairly easy to generate a multilevel menu using nested HTML lists, dynamically from a database table, and with only a single query – given a simple helper, recursive function to work through a multidimensional array.
given a similar database table structure to that in the first link above, how can this be accomplished with Yii2?
Basically the same way as in the linked example, with the following differences:
- Instead of two separate functions, you might want to create a custom widget class which is responsible for both building the database query and rendering the result.
- Instead of mysql_* functions, yii\db\* should be used for database access.
- yii\helpers\Html can simplify the rendering process a bit (less string concatenations).
I think this is quite close to what I need: github.com/Patroklo/yii2-menu
"Menu widget that loads Kartik sidenav widget with database items."
and in turn, Kartik sidnav extends the Yii Menu widget.
One limitation is that Kartik sidenav CSS supports only up to 3 levels of submenus, so CSS would need to be customized to support 4-levels, per my needs.
and since the widget outputs HTML lists (with some styling), I think it shouldn’t be too difficult to integrate with something like smartmenus.org