title: How do I pass a global variable in Main layout page before $content in Yii2
body: <p>I am trying to create dynamic menu in yii2 using "Nav::widget". Here is my code in menu section in main layout page:</p>
  
  <pre><code>    echo Nav::widget([
              'options' =&gt; ['class' =&gt; 'navbar-nav navbar-right'],
              'items' =&gt; [
                  ['label' =&gt; 'Home', 'url' =&gt; ['/site/index']],
                  ['label' =&gt; 'About', 'url' =&gt; ['/site/about']],
  </code></pre>
  
  <p>Trying to get the solution: Please have a look::</p>
  
  <p>1 I have created a super controller "components/Controller.php" in app:</p>
  
  <pre><code>namespace app\components;
  use app\models\MenuPanal;
  
  class Controller extends \yii\web\Controller
  {
  
     public $menuItems = [];
  
  public function init(){
  
       $items = MenuPanal::find()
          -&gt;orderBy('id')
          -&gt;all();
  
       $menuItems = [];
       foreach ($items as $key =&gt; $value) {
                   $this-&gt;menuItems[] = ['label' =&gt; $value['c_type'] , 'url' =&gt; ['#']];
              }
  
     parent::init();
    }
  }
  </code></pre>
  
  <p>2 Place in the Main Layout Page ::</p>
  
  <pre><code>   echo Nav::widget([
              'options' =&gt; ['class' =&gt; 'navbar-nav navbar-right'],
  
              'items' =&gt; Yii::$app-&gt;controller-&gt;menuItems,
  
          ]);
  </code></pre>
  
  <p>Helps are highly appreciated.</p>