【问题标题】:From Backend to Frontend Yii2 Advanced App从后端到前端 Yii2 高级应用
【发布时间】:2015-09-10 08:57:11
【问题描述】:

我正在尝试将一些控制器从前端链接到后端。几个小时后,我不知道问题出在哪里。

后端

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

还给我

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

有什么建议吗?

【问题讨论】:

  • @InsaneSkullll 当我尝试重定向到前端时,Yii 说我 Page not found 。我被卡住了,我想知道如何解决它
  • 你不需要Url::to()
  • 看看我的解决方案来解决你的问题。
  • 您好@JorgeBrage,请发布您的解决方案作为答案。

标签: hyperlink yii2 yii2-advanced-app


【解决方案1】:

您的代码是正确的。 urlManagerFrontEnd 应该返回基于 baseUrl /frontend/web 的 url。

尝试将baseUrl更改为http://yourdomain/

【讨论】:

  • 我猜我有这个问题,因为它托管在 localhost 所以,最后我以这种方式解决了它。 ` const ROLE_ADMIN = 0; public function beforeAction($event) { // 用户变量 $user = \common\models\User::findIdentity(Yii::$app->user->id); if (is_null($user) == false) { if ($user->role == self::ROLE_ADMIN) { return $this->redirect(["site/logout"]); } } 返回父级::beforeAction($event); } `
【解决方案2】:

我做了一点谷歌搜索,找到了this link。

作为参考,我在这里发布相同的内容

我在 UrlManager.php 中阅读并发现以下内容:

$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();

所以这意味着当showScriptName= true 和enablePrettyUrl=false $baseUrl = getScriptUrl() 否则$baseUrl = getBaseUrl()

所以它只适用于prettyUrl=true 和showScriptName = false。当我们将 prettyUrl 设置为 true 时,它​​需要 $baseUrl = getBaseUrl() 将其更改为以下内容即可解决我们的问题 =)。

/*$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();*/

$baseUrl = !$this->showScriptName || $this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();

现在您必须将prettyurl=false 和另一个设置为 true et voila

我在一个新模板上尝试了这个,然后应用了你提到的代码,得到了和你一样的错误。

但是在我根据这篇文章进行修复之后,我得到了正确的路径。

This link 也很有帮助。

【讨论】:

    【解决方案3】:

    在您的前端配置中将其添加到顶部以定义 2 个变量。

    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参数

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

    然后您可以通过例如从前端到后端的链接

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

    要从后端到前端具有相同的功能,请将其添加到后端配置中:

    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);
    

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

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

    顺便说一句。如果您通过某些 htaccess 删除了 /web 行为,您还应该在变量中删除它。

    【讨论】:

      【解决方案4】:

      使用此代码。它会将您重定向到前端

      return $this->redirect(Yii::$app->urlManager->createUrl('./../../frontend/web/'));
      

      【讨论】:

        【解决方案5】:

        使用下面的一个:

        Url::to(Yii::$app->urlManagerBackEnd->createUrl('index.php/'/site/index'), true);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多