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([
\'o
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,
]);