Yii2 foreach 在 $menuItems 中循环
Yii2 foreach loop inside $menuItems
我有这样的菜单
<?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 循环将所有团队列为下拉菜单供来宾用户查看,但它没有用。
请帮我解决一下这个。对不起,我的英语不好。
谢谢。
这可能不是最好的方法,但对我有用。
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)
];
希望对您有所帮助,
我有这样的菜单
<?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 循环将所有团队列为下拉菜单供来宾用户查看,但它没有用。 请帮我解决一下这个。对不起,我的英语不好。 谢谢。
这可能不是最好的方法,但对我有用。
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)
];
希望对您有所帮助,