0

背景

我正在使用 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?

4

3 回答 3

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);
}
于 2015-03-31T21:10:57.323 回答
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);
}
于 2013-09-09T06:17:06.673 回答
0

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

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

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

$this->redirect(Yii::app()->createAbsoluteUrl('user/login'));
于 2013-09-08T20:31:11.190 回答