How do I pass a global variable in Main layout page before $content in Yii2

前端 未结 1 1059
挽巷
挽巷 2021-01-06 22:54

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         


        
相关标签:
1条回答
  • 2021-01-06 23:04

    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,
    ]);
    
    0 讨论(0)
提交回复
热议问题