I want to display menu items dynamically from database also static in my yii2 frontend

1.3k Views Asked by At

I just want to display menus from database in yii2 advanced template frontend. Also i have static menus. I am using menu widget

Here is my code

           <?php
              echo Menu::widget([
                'options' => ['class' => 'about_content'],
                'items' => CMS::getCMSPages(),
              ]);
            ?>

Here CMS::getCMSPages() will get the menus from database. And also i have static menu. So i added into the menu widget like this

           <?php
              echo Menu::widget([
                'options' => ['class' => 'about_content'],
                'items' => [[CMS::getCMSPages()],
                     ['label' => 'contact', 'url' => ['site/index']]
                 ]

              ]);
            ?>

But this is not working. Someone help me guys

1

There are 1 best solutions below

2
On BEST ANSWER

CMS::getCMSPages() method should return properly prepared array of items. Something like this:

[
    ['label' => 'Home', 'url' => ['site/index']],
    ['label' => 'Products', 'url' => ['product/index'],
]

Also you should merge items array:

<?php
  echo Menu::widget([
    'options' => ['class' => 'about_content'],
    'items' => array_merge(CMS::getCMSPages(), [['label' => 'contact', 'url' => ['site/index']]])
  ]);
?>