YII2:如何在菜单项中创建链接/URL以指向外部网页,而不是应用程序的操作或视图 [英] 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

查看:362
本文介绍了YII2:如何在菜单项中创建链接/URL以指向外部网页,而不是应用程序的操作或视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用YII2菜单小部件,但找不到在创建的链接上添加class,target等属性选项的解决方案.

I am using the YII2 Menu Widget and did not find the solution to add attribute options like class , target on created link.

我的代码如下:

echo Menu::widget(
[
    'options' => [
        'class' => 'sidebar-menu'
    ],

    'items' => [

        [
            'label' => Yii::t('backend', 'Admin'),
            'url' => Yii::$app->homeUrl,
            'icon' => 'fa-list-alt',
            'options' => [
                'class' => 'treeview',
            ],
            'items' => [
                [
                    'label' => Yii::t('backend', 'External link'),
                    'url' => 'http://google.com',
                    'icon' => 'fa-list-alt',
                    'options' => [
                    'target' => '_blank',
                    ],
                ],
            ]
        ],                

    ]
]
 );

选项目标未添加到生成的链接上.

Option target is not added on generated link.

推荐答案

通过模板设置如下添加目标.您在代码中设置的选项是li元素的"HTML选项",而不是链接选项.

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>',
    ],
]

这篇关于YII2:如何在菜单项中创建链接/URL以指向外部网页,而不是应用程序的操作或视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆