向Yii2导航栏添加图标

时间:2019-03-11 18:52:33

标签: php css twitter-bootstrap yii yii2

我对Yii2并不陌生,我目前正在使用内置窗口小部件构建基本的导航栏。我唯一遇到的问题是我不确定如何在相关菜单选项旁边添加图标。我当前的导航栏如下所示:

  NavBar::begin([
  'brandLabel' => Html::img('@web/images/logo.png', ['alt'=>Yii::$app->name,'style'=>'height: 50px; width: 171px; top: 15px; left: 50px; position: absolute;']),
  'brandUrl' => Yii::$app->homeUrl,
  'options' => [
      'class' => 'navbar navbar-fixed-top',
  ],
 ]);
echo Nav::widget([
  'options' => ['class' => 'navbar-nav','style'=>'padding-left:45%;padding-top:35px;'],
  'items' => [
      ['label' => 'How It Works', 'url' => ['/site/index']],
      ['label' => 'Products', 'url' => ['/site/about']],
      ['label' => 'Pricing', 'url' => ['/site/about']],
      ['label' => 'Contact', 'url' => ['/site/contact']],
      '<li class="line" style="padding-top:5px;padding-right:10px;">|</li>',
      // '<i class="glyphicon glyphicon-inbox" style="right:70px;top:7px;"></i>',
      '<i class="glyphicon glyphicon-lock" style="right:70px;top:7px;"></i>',
      Yii::$app->user->isGuest ? (
          ['label' => 'Login','class'=>'login', 'url' => ['/site/login'],]
      ) : (
          '<li>'
          . Html::beginForm(['/site/logout'], 'post')
          . Html::submitButton(
              'Logout (' . Yii::$app->user->identity->username . ')',
              ['class' => 'btn btn-link logout']
          )
          . Html::endForm()
          . '</li>'
      )
  ],
]);
NavBar::end();

正如您在此处看到的,我正在添加锁定图标并将其定位在登录选项旁边。有没有办法让我将该图标嵌入链接中,以便它们对悬停做出同样的反应?我正在寻找符合以下条件的东西:

<a href="/site/login"><i class="glyphicon glyphicon-lock"></i>Login</a>.

但是我目前有:

<i class="glyphicon glyphicon-lock" style="right:70px;top:7px;"></i>
<a href="/site/login">Login</a>

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

据我了解,您想在导航文本中显示如下所示的锁定图标

enter image description here

如果这是正确的,则您没有将图标添加到正确的位置,应该在label属性内添加图标html,然后在'encodeLabels' => false,小部件中设置Nav

您的代码应如下所示

NavBar::begin(
    [
        'brandLabel' => Html::img('@web/images/logo.png', ['alt' => Yii::$app->name, 'style' => 'height: 50px; width: 171px; top: 15px; left: 50px; position: absolute;']),
        'brandUrl' => Yii::$app->homeUrl,
        'options' => [
            'class' => 'navbar navbar-fixed-top'
        ]
    ]
);
echo Nav::widget(
    [
        'encodeLabels' => false,
        'options' => ['class' => 'navbar-nav', 'style' => 'padding-left:45%;padding-top:35px;'],
        'items' => [
            ['label' => 'How It Works', 'url' => ['/site/index']],
            ['label' => 'Products', 'url' => ['/site/about']],
            ['label' => 'Pricing', 'url' => ['/site/about']],
            ['label' => 'Contact', 'url' => ['/site/contact']],
            '<li class="line" style="padding-top:5px;padding-right:10px;">|</li>',
            Yii::$app->user->isGuest ? (
                ['label' => '<i class="glyphicon glyphicon-lock"></i>&nbsp;Login', 'class' => 'login', 'url' => ['/site/login']]
            ) : (
                '<li>'
                . Html::beginForm(['/site/logout'], 'post')
                . Html::submitButton(
                    'Logout (' . Yii::$app->user->identity->username . ')',
                    ['class' => 'btn btn-link logout']
                )
                . Html::endForm()
                . '</li>'
            )
        ]
    ]
);
NavBar::end();