从后端到前端Yii2高级应用程序

编程入门 行业动态 更新时间:2024-10-26 00:31:25
本文介绍了从后端到前端Yii2高级应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将一些控制器从前端链接到后端。几个小时之后我不会出现问题。

I'm trying to link some controllers from frontend to backend. After some hours I don't where could be the problem.

后端

file: main.php 'urlManager' => [ 'enablePrettyUrl' => false, 'showScriptName' => false, 'baseUrl' => '/backend/web', ], 'urlManagerFrontEnd' => [ 'class' => 'yii\web\urlManager', 'baseUrl' => '/frontend/web', 'enablePrettyUrl' => false, 'showScriptName' => false, ] file: SiteController.php public function actionIndex() { // User's variable $user = \common\models\User::findIdentity(Yii::$app->user->id); if($user->role != self::USER_ADMIN){ return $this->redirect(Url::to(Yii::$app->urlManagerFrontEnd->createUrl(['/site/index']))); } return $this->render('index'); }

使用此

Url :: to(Yii :: $ app-> urlManagerFrontEnd-> createUrl(['/ site / index']))

Url::to(Yii::$app->urlManagerFrontEnd->createUrl(['/site/index']))

返回我

/advanced/backend/web/index.php?r=site%2Findex

/advanced/backend/web/index.php?r=site%2Findex

有什么建议吗?

推荐答案

in你的前端配置将它添加到顶部以定义2个变量。

in your frontend config add this to the top to define 2 variables.

use \yii\web\Request; $baseUrl = str_replace('/frontend/web', '/frontend/web', (new Request)->getBaseUrl()); $backEndBaseUrl = str_replace('/frontend/web', '/backend/web', (new Request)->getBaseUrl());

并将这些变量设置为组件中的baseUrl参数

And set these variables as the baseUrl parameters in the components

'components' => [ 'urlManager' => [ 'class' => 'yii\web\urlManager', 'enablePrettyUrl' => false, 'showScriptName' => false, //'baseUrl' => '/frontend/web', 'baseUrl'=> $baseUrl, ], 'urlManagerBackEnd' => [ 'class' => 'yii\web\urlManager', 'enablePrettyUrl' => false, 'showScriptName' => false, //'baseUrl' => '/backend/web', 'baseUrl' => $backEndBaseUrl, ],

然后你可以拥有从前端到后端的链接例如

then you can have links from the frontend to the backend by e.g.

$backendUrl= Yii::$app->urlManagerBackEnd->createUrl('//'); echo yii\helpers\Html::a('link to backend', $backendUrl);

从后端到前端的相同内容将其添加到后端配置中:

to have the same from the backend to the frontend add this to the backend config:

use \yii\web\Request; $baseUrl = str_replace('/backend/web', '/backend/web', (new Request)->getBaseUrl()); $frontEndBaseUrl = str_replace('/backend/web', '/frontend/web', (new Request)->getBaseUrl());

以及组件中:

'urlManager' => [ 'class' => 'yii\web\urlManager', 'enablePrettyUrl' => false, 'showScriptName' => false, 'baseUrl'=> $baseUrl, ], 'urlManagerFrontEnd' => [ 'class' => 'yii\web\urlManager', 'enablePrettyUrl' => false, 'showScriptName' => false, //'baseUrl' => '/backend/web', 'baseUrl' => $frontEndBaseUrl, ],

并创建链接使用:

$frontendUrl= Yii::$app->urlManagerFrontEnd->createUrl('//'); echo yii\helpers\Html::a('link to frontend', $frontendUrl);

忘了你当然也可以链接到特定页面,例如从后端到前端网站/关于:

forgot you can of course also link to specific pages e.g. from backend to frontend site/about:

$frontendUrl= Yii::$app->urlManagerFrontEnd->createUrl('/site/about'); echo yii\helpers\Html::a('link to frontend site about', $frontendUrl);

BTW。如果你已经删除某些htaccess的/ web行为,你也应该在变量中删除它。

BTW. if you have removed the /web behavior by some htaccess you should also remove it in the variables.

更多推荐

从后端到前端Yii2高级应用程序

本文发布于:2023-11-01 19:53:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550253.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   端到   高级

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!