YII2: How can I create a link/url in a Menu item to point to an external webpage instead of an action or view of my application

耗尽温柔 提交于 2019-12-08 13:03:33

add the target like below through the template setting. The Options you have set in your code are the Html Options of the li element and not the link options.

'items' => [
    [
       'label' => Yii::t('backend', 'External link'),
       'url' => 'http://google.com',
       'icon' => 'fa-list-alt',
       'template'=> '<a href="{url}" target="_blank">{label}</a>',
    ],
]

Suggestions above do not seem to be working (in my case), an alternative solution is:

'linkOptions' => ['target' => '_blank']

For example

[
    'url' => \Yii::$app->user->identity->getBlogLink(),
    'linkOptions' => ['target' => '_blank']  ,
    'label' => \Yii::t('app', 'Blog')
]

If you want to keep the icon in your menu:

'template'=> '<a href="{url}" target="_blank">{icon}{label}</a>'

As regards the class you must specify it in the options key:

[
    'label' => 'Debug', 
    'icon' => 'fa fa-dashboard', 
    'url' => ['/debug'],
    'options' => ['class' => 'special'],
    'template'=> '<a href="{url}" target="_blank">{icon}{label}</a>',
],

gives the following menu:

<li class="special">
    <a target="_blank" href="your_site_url_here/index.php?r=debug">
        <i class="fa fa-dashboard"></i>
        <span>Debug</span>
    </a>
</li>

You can add any url. For Example,

echo Menu::widget([
    'items' => [
        ['label' => 'Home', 'url' => ['http://www.google.com']],
        ['label' => 'About', 'url' => ['site/about']],
     ['label' => 'Contact', 'url' => ['site/contact']],
    ],
    'options' => [
                    'class' => 'navbar-nav nav',
                    'id'=>'navbar-id',
                    'style'=>'font-size: 14px;',
                    'data-tag'=>'yii2-menu',
                ],
]);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!