yii2 Framework通过菜单小部件中的公共变量获取项目

时间:2016-03-28 17:22:47

标签: yii2

我正在使用yii2菜单小部件,我需要通过变量获取菜单项。例如:

Menu::widget([
'items' => $items,
'activeCssClass'=>'active',
'encodeLabels' => false,// Requested feature
'options' => [
    'class' => 'nav nav-tabs',
],

]);

如何在面包屑中调用公共变量suh为$this->params['breadcrumbs']

 Breadcrumbs::widget([
'homeLink' => [
    'label' => '<i class="fa fa-home"></i> ',
    'url' => Yii::$app->homeUrl,
    'encode' => false// Requested feature
],
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],

])

2 个答案:

答案 0 :(得分:0)

如果你有

<?php 

   $this->params['breadcrumbs'][] = ['label' => 'Your_labe', 'url' => ['your-controller/your-action']];
   // then   
   $menuItems[] = $this->params['breadcrumbs'];

   // OR  you can set $menuItems this way 
   // $menuItems[] =  ['label' => 'Your_labe', 'url' => ['your-controller/your-action']];

    Menu::widget([
    'items' => $menuItems,
    'activeCssClass'=>'active',
    'encodeLabels' => false,// Requested feature
    'options' => [
        'class' => 'nav nav-tabs',
    ],

?> 

如果你想在布局中使用菜单小部件,你可以试试这种方式

在视图中,您可以通过这种方式分配商品

   $this->params['menu_items'][] = ['label' => 'Your_labe', 'url' => ['your-controller/your-action']];

在布局中,您可以这样使用这些项目

    Menu::widget([
    'items' => isset($this->params['menu_items']) ? $this->params['menu_items'] : [],
    'items' => $menuItems,
    'activeCssClass'=>'active',
    'encodeLabels' => false,// Requested feature
    'options' => [
        'class' => 'nav nav-tabs',
    ],

答案 1 :(得分:0)

感谢您的帮助,我修复了问题: 在main.php中

Menu::widget([
'items' => isset($this->params['menu']) ? $this->params['menu'] : [],
'activeCssClass'=>'active',
'encodeLabels' => false,// Requested feature
'options' => [
    'class' => 'nav nav-tabs',
],

]);

并在view.php中调用

$this->params['menu'] =[
['label' => '<i class="fa fa-list-ol alis"></i> List',
    'url' => ['site/index'],
],
['label' => '<i class="fa fa-plus alis"></i> Create',
    'url' => ['site/create'],
],
['label' => '<i class="fa fa-search alis"></i> View',
    'url' => ['site/view'],
],

];