YII重定向子域

时间:2013-09-08 11:08:10

标签: php redirect yii subdomain

背景

我正在使用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-&gt; forward()转到另一个子域时,我最终会进入同一个子域。

实施例

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

$这 - &GT;正向(Yii的::应用程序() - &GT; createAbsoluteUrl( '用户/登录'));

问题

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

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

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