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 に答える