Question

I want to create a dynamic menu with my table (db). I have followed some instructions which are given below:

Table : "menupanal"

Table :

Step 01: I just create a super controller in app\components\Controller.php

Here is the code:

    namespace app\components;

use app\models\MenuPanal;

class Controller extends \yii\web\Controller
{

    public $menuItems = [];

    public function init(){

     $items = MenuPanal::find()
        ->where(['c_type'  => 'MENU'])
        ->orderBy('id')
        ->all();


     $menuItems = [];
     foreach ($items as $key => $value) {
                 $this->menuItems[] = 
                      ['label' => $value['c_name'], 
                          'items'=> [
                              ['label' => $value['c_redirect'], 'url' => ['#']],
                          ],
                      ];    
            }

  parent::init();
}

Step 02: Changed n main layout page:

        echo Nav::widget([
            'options' => ['class' => 'navbar-nav navbar-right'],

            'items' => Yii::$app->controller->menuItems,
        ]);

It is working in only one level. My question::

Question : how can I add multilevel menu using Super controller ?

I am new in Yii2. Helps are highly appreciated.

Was it helpful?

Solution

  1. Create New MenuHelper in Component folder. There is no default component folder. Please create by yourself.

    <?php
    
    namespace app\components;
    
    use app\models\MenuPanel;
    use app\models\Zuser;
    use app\models\Vwrole;
    use app\assets\AppAsset;
    
    
    class MenuHelper
    {
    
        public static function getMenu()
        {
            $role_id = 1;
            $result = static::getMenuRecrusive($role_id);
            return $result;
        }
    
        private static function getMenuRecrusive($parent)
        {
    
            $items = MenuPanel::find()
                ->where(['c_parentid' => $parent])
                ->orderBy('c_sortord')
                ->asArray()
                ->all();
    
            $result = []; 
    
            foreach ($items as $item) {
                $result[] = [
                        'label' => $item['c_name'],
                        'url' => ['#'],
                        'items' => static::getMenuRecrusive($item['id']),
                        '<li class="divider"></li>',
                    ];
            }
            return $result;
        }
    
    }
    
  2. in Main Layout Page put the following code

        echo Nav::widget([
            'options' => ['class' => 'navbar-nav navbar-right'],
            'items' => app\components\MenuHelper::getMenu(),
    

Enjoy Coding!!

OTHER TIPS

You may use nested sets. Look at this extension for Yii: http://www.yiiframework.com/extension/nestedsetbehavior/ and its documentation. All you need to do is component with dynamic creation of menu items array for nested sets.

I found that there is a Yii2 extension version: http://www.yiiframework.com/extension/yii2-nestedsetbehavior/

Good luck

You may use this extension for multilevel dropdownMulti level Dropdown

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top