前端和后端之间的Yii2链接(高级模板) [英] Yii2 Links between Frontend and Backend (advanced template)

查看:99
本文介绍了前端和后端之间的Yii2链接(高级模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要从菜单中的后端部分(或从后端到管理员)中添加指向前端东西的链接,我该如何在没有硬编码的情况下做到这一点? 这个:

If i need add links to frontend stuff from backend part in menu(or from backend to admin), how i can do this without hardcode? This:

 \Yii::$app->request->BaseUrl 

返回父目录的路径

/sitename/backend/web
/sitename/frontend/web

推荐答案

在后端应用程序配置中,您应该添加其他"UrlManager"组件,其名称和配置应与前端应用程序中使用的组件相同:

In your backend application config you should add additional 'UrlManager' component with different name and configuration equals to the one used at front end application:

return [
    'components' => [
        'urlManager' => [
            // here is your backend URL rules
        ],
        'urlManagerFrontEnd' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => '/a/frontend/web',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],

    ],
];

然后,您应该调用以下内容来组成前端URL:

Then you should invoke following to compose front-end URL:

Yii::$app->urlManagerFrontEnd->createUrl();

这篇关于前端和后端之间的Yii2链接(高级模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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