【问题标题】:YII redirect with subdomainYII 使用子域重定向
【发布时间】:2013-09-08 11:08:10
【问题描述】:

背景

我正在使用 Yii 创建一个 Web 应用程序,我正在使用子域将站点和支持文件与我的应用程序分开。我正在像这样映射我的子域。

'urlManager'   => array(
        'urlFormat' => 'path',
        'rules'     => array(
            /* gii for developement, remove this in production */
            'gii'                                                                   => 'gii',
            'gii/<controller:\w+>'                                                  => 'gii/<controller>',
            'gii/<controller:\w+>/<action:\w+>'                                     => 'gii/<controller>/<action>',

            /* Sub domain mapping */
            'http://<module:\w+>.<hostname:[^\/]+>/'                                         => '<module>/',
            'http://<module:\w+>.<hostname:[^\/]+>/<controller:\w+>/<id:\d+>'                => '<module>/<controller>/view',
            'http://<module:\w+>.<hostname:[^\/]+>/<controller:\w+>/<action:\w+>/<id:\d+>'   => '<module>/<controller>/<action>',
            'http://<module:\w+>.<hostname:[^\/]+>/<controller:\w+>/<action:\w+>'            => '<module>/<controller>/<action>',

            /* Website URL Mappint */
            '<controller:\w+>/<id:\d+>'                                             => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'                                => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>'                                         => '<controller>/<action>',
        ),
    ),

这一切都按预期工作,我的问题是当我需要在模块之间重定向时。每当我尝试 $this->forward() 到另一个子域时,我最终都会进入同一个子域。

示例

在 app.examplesite.com 上时,我尝试转发到 www.examplesite.com/user/login,但最终到达 app.examplesite.com/user/login

$this->forward(Yii::app()->createAbsoluteUrl('user/login'));

问题

如何正确地从 app.examplesite.com 重定向到 www.examplesite.com/user/login?

【问题讨论】:

  • 这只是一个建议(我知道这个问题有点老了),但您可以试试:$this-&gt;forward(Yii::app()-&gt;createAbsoluteUrl('/user/login')); 请注意,您要重定向到的路径前有一个斜杠
  • 嗨 ragingprodigy,使用 $this-&gt;forward(Yii::app()-&gt;createAbsoluteUrl('/user/login')); 不会更改模块,它只会导航到执行代码的模块的 /user/login。因此,如果从 app.examplesite.com 上的控制器调用它,它将重定向到 app.examplesite.com/user/login

标签: php redirect yii subdomain


【解决方案1】:

我不确定这是否是最优雅的解决方案,但它似乎对我有用。我扩展了 Controller 并添加了一个额外的 subdomainRedirect 方法,该方法允许我另外传递我想要重定向到的子域。这将允许我在我的控制器中做这样的事情。 $this->subdomainRedirect('user/login', 'www');或 $this->subdomainRedirect('/', 'admin');

/**
 * Redirects the browser to the specified URL using another subdomain, controller and action.
 * This is similar to redirect however allows the selection of the subdomain to use when redrecting.
 *
 * @param mixed  $url        the URL to be redirected to. If the parameter is an array, the first element must be a route to a controller action and the rest are GET parameters in name-value pairs.
 * @param string $subdomain The subdomain to include in the redirect url, defaults to www.
 * @param bool   $terminate  whether to terminate the current application after calling this method
 * @param int    $statusCode the anchor that should be appended to the redirection URL. Defaults to empty. Make sure the anchor starts with '#' if you want to specify it.
 */
public function subdomainRedirect($url, $subdomain = 'www', $terminate = true, $statusCode = 302) {
    preg_match("/^(?<protocol>(http|https):\/\/)(((?<subdomain>[a-z]+)\.)*)((.*\.)*(?<domain>.+\.[a-z]+))$/", Yii::app()->request->hostInfo, $matches);

    if ($url[0] !== '/') {
        $url = '/' . $url;
    }

    $url = $matches['protocol'] . $subdomain . '.' . $matches['domain'] . $url;
    $this->redirect($url, $terminate, $statusCode);
}

【讨论】:

    【解决方案2】:
     public function subRedirect($url, $subdomain = 'www', $terminate = true, $statusCode = 302)
    {
        preg_match("/^(?<protocol>(http|https):\/\/)(((?<subdomain>[a-z]+)\.)*)((.*\.)*(?<domain>.+\.[a-z]+))$/", Yii::app()->request->hostInfo, $matches);
        // $url is in format array(controlerID/actionID) 
        $params = $url;
        unset($params[0]);
        $final = $this->createUrl($url[0], $params);
        $url = $matches['protocol'] . $subdomain . '.' . $matches['domain'] . $final;
        $this->redirect($url, $terminate, $statusCode);
    }
    

    【讨论】:

      【解决方案3】:

      我想如果你输入这样的记录:

      'www.examplesite.com/user/login'=>'/user/login'
      

      然后你可以从任何地方调用它

      $this->redirect(Yii::app()->createAbsoluteUrl('user/login'));
      

      【讨论】:

      • 感谢 Zack,但这似乎不起作用。重定向仍将是 app.example.com/user/login,因此条目记录永远不会匹配。
      • 很奇怪,我在我的系统上测试了这个,在实施之前,我遇到了和你一样的问题,在实施之后问题就消失了,这表明该解决方案有效。我应该注意到我无法让它与 forward 一起工作,请注意在我的回答中使用了重定向。
      • 每当我从子域中使用 Yii::app()->createAbsoluteUrl() 时,它都会在 URL 中包含当前子域。此外,我尝试更改条目记录的顺序,但仍然没有成功。我最终只是创建了自己的方法来做我想做的事,请参阅我附加的答案。
      猜你喜欢
      • 2013-04-23
      • 2017-08-24
      • 2011-02-02
      • 1970-01-01
      • 2021-10-21
      • 2014-08-19
      • 2020-11-28
      • 2013-08-10
      相关资源
      最近更新 更多