NavBar项目中的访问控制器变量

时间:2017-09-05 10:59:39

标签: yii2 yii2-advanced-app yii2-basic-app

在导航栏的布局文件中,我有一个名为message的项目。我想在它上面显示消息的数量实际上我可以构建一个隐藏的元素和acho我的变量在它里面并再次获取它并将其附加到项目中的安全元素与Jquery ...但我想知道更简单和更短的方式,如果有是任何

我在beforeAction中得到了这样的消息计数

 namespace app\component;

    class Controller extends \yii\web\Controller {

        public function beforeAction($event)
        {

            $this->layout = "site-developer";

            $username = Yii::$app->user->identity->username;
            $model = Messages::find()->select('*')->where(['towho' => $username])->count();

            $this->view->params['model'] = $model;


            return parent::beforeAction($event);
        }


    }
布局中的

我的导航栏代码就像这样

<?php
NavBar::begin([
    'brandOptions' => [
        'class' => 'hide-on-med-and-down'
    ],
    'brandLabel' => 'My Company',
    'brandUrl' => Yii::$app->homeUrl,
    'fixed' => false,
    'wrapperOptions' => [
        'class' => 'nav-wrapper'
    ],
]);

$menuItems = [
    ['label' => '<i class="material-icons">search</i>'],
    ['label' => '<i class="material-icons">email</i>'],
    ['label' => '<i class="material-icons">view_module</i>'],
    ['label' => '<i class="material-icons">refresh</i>'],
    ['label' => '<i class="material-icons">more_vert</i>', 'items' => [  // will be rendered as dropdown
        ['label' => 'Home', 'url' => ['/site/index']],
        ['label' => 'About', 'url' => ['/site/about']],
        ['label' => 'Home', 'url' => ['/site/about']],
        ['label' => 'Contact', 'url' => ['/site/contact']]
    ]],

];
if (Yii::$app->user->isGuest) {
    $menuItems[] = ['label' => '<i class="fa fa-sign-in"></i>', 'url' => ['/site/signup'], 'class'=>'aa'];
    $menuItems[] = ['label' => '<i class="fa fa-sign-in"></i>', 'url' => ['/site/login'], 'class'=>'aa'];
} else {
    $menuItems[] = '<li>'
        . Html::beginForm(['/site/logout'], 'post')
        . Html::submitButton(
            '<i class="fa fa-sign-out"></i> (' . Yii::$app->user->identity->username . ')',
            ['class' => 'badge red lighten-2 user-dash']
        )
        . Html::endForm()
        . '</li>';
}

echo Nav::widget([
    'encodeLabels' => false,
    'options' => ['class' => 'right'],
    'items' => $menuItems,

]);

NavBar::end();
?>

这里有导航栏菜单项中用户号码的任何方式         的 ['label' => '<i class="material-icons">email</i>'],

???

我们可以使用

访问该号码
<?php

$model = $this->params['model'];

echo $model; ?>

1 个答案:

答案 0 :(得分:1)

您在'encodeLabels' => false, Nav::widget已经可以在标签内容中连接值<{1}}

['label' => '<i class="material-icons">email(' . $this->params['model']. ')</i>'],