Yii2 前后端链接(高级模板)

2022-01-07 00:00:00 php 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' => 'yiiweburlManager',
            'baseUrl' => '/a/frontend/web',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],

    ],
];

那么您应该调用以下内容来编写前端 URL:

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

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

相关文章