2

I am trying to create dynamic menu in yii2 using "Nav::widget". Here is my code in menu section in main layout page:

    echo Nav::widget([
            'options' => ['class' => 'navbar-nav navbar-right'],
            'items' => [
                ['label' => 'Home', 'url' => ['/site/index']],
                ['label' => 'About', 'url' => ['/site/about']],

Trying to get the solution: Please have a look::

1 I have created a super controller "components/Controller.php" in app:

namespace app\components;
use app\models\MenuPanal;

class Controller extends \yii\web\Controller
{

   public $menuItems = [];

public function init(){

     $items = MenuPanal::find()
        ->orderBy('id')
        ->all();

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

   parent::init();
  }
}

2 Place in the Main Layout Page ::

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

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

        ]);

Helps are highly appreciated.

1 Answers1

6

You could, for example, create your own super controller and add a menuItems attribute :

namespace app\components;

class Controller extends \yii\web\Controller
{
    public $menuItems = [
        ['label' => 'Home', 'url' => ['/site/index']],
        ['label' => 'About', 'url' => ['/site/about']]
    ];
}

Your controllers should extend it :

namespace app\controllers;

use app\components\Controller;

class MyController extends Controller {...}

And in your layout :

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => Yii::$app->controller->menuItems,
]);
soju
  • 25,111
  • 3
  • 68
  • 70
  • thanks for your reply. But in yii2 basic there is no "components" folder. In that case can I use "commands" ?? Please –  Jun 02 '14 at 16:17
  • 2
    You juste have to create it. – soju Jun 02 '14 at 16:22
  • Dear, I got fix that issue BUT got another issue for $app for login/ logut checking user :: ERROR::: PHP Parse Error – yii\base\ErrorException syntax error, unexpected '$app' (T_VARIABLE), expecting identifier (T_STRING) –  Jun 02 '14 at 17:40
  • AND how can I use foreach for menu(from db) and Place in main layout page ? please –  Jun 02 '14 at 18:03
  • 2
    add public function init(){} for Controller and fetch menuitems from DB into public menuItems, and at backend make simple CRUD for these menuitems – user1852788 Jun 03 '14 at 03:56
  • It is not working.. I got a little solution though it is not working: I have updated my code... Please have a look. –  Jun 03 '14 at 08:30
  • 1
    you forget public $menuItems = []; before public init() in your Controller (check answer code). And in your foreach(){} replace $menuItems with $this->menuItems. – user1852788 Jun 03 '14 at 08:50
  • @soju : that was a mistake by clicking. Apologies for that. I didn't look on it. I didn't change all my questions. I just added my new steps regarding my question. Please have a look –  Jun 03 '14 at 09:39
  • @sujo :: IT IS WORKING PERFECTLY. Thank you so much. I embarrassed you with my idiots code. THANKS AGAIN. And apologies –  Jun 03 '14 at 10:51
  • @user1852788 :: IT IS WORKING PERFECTLY. Thank you so much. I embarrassed you with my idiots code. THANKS AGAIN. And apologies –  Jun 03 '14 at 10:52