在$ menuItems

时间:2017-03-25 04:27:36

标签: yii2 yii2-basic-app

我有这样的菜单

<?php
NavBar::begin([
    'brandLabel' => Html::img('@web/images/cennos1.png', ['alt'=>Yii::$app->name]),
    'brandUrl' => Yii::$app->homeUrl,
    'brandOptions' => ['style' => 'margin-top:-7px;'],
    'options' => [
        'class' => 'navbar-inverse navbar-fixed-top',
    ],
]);  
$menuItems = [];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
$menuItems[] = [
                    'label' => 'Teams',
                    'items' => [        
                        foreach ($teams as $team) {
                        ['label' => '' . $team->name .'', 'url' => ['team/preview','id' => $team->id]],
                        }
                    ],
                ];

我尝试使用这样的foreach循环列出所有团队作为访客用户查看的下拉菜单,但它不起作用。 请帮我解决一下这个。对不起,我的英语不好。 谢谢。

1 个答案:

答案 0 :(得分:0)

这可能不是最好的方式,但它对我有用。

function items($teams)
    {
        $items = [];
        foreach ($teams as $team) {
            array_push($items, ['label' => '' . $team->name .'', 'url' => Url::to(['team/preview', 'id' => $team->id])]);
        }
        return $items;
    }

NavBar::begin([
    'brandLabel' => Html::img('@web/images/cennos1.png', ['alt'=>Yii::$app->name]),
    'brandUrl' => Yii::$app->homeUrl,
    'brandOptions' => ['style' => 'margin-top:-7px;'],
    'options' => [
        'class' => 'navbar-inverse navbar-fixed-top',
    ],
]);  
$menuItems = [];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
$menuItems[] = [
                    'label' => 'Teams',
                    'items' => items($teams)
                ];

希望它有所帮助,