Yii框架和dinamic子域

时间:2017-04-12 16:32:25

标签: php yii subdomain

我想知道Yii框架如何根据URL中传递的子域将请求发送到正确的控制器:

www.mysystem.com - >此请求由我系统中特定模块的默认控制器处理。

但是当用户访问他的商店时,他将使用URL:storename.mysystem.com。 (有许多不同的商店名称)

我想知道Yii在哪里可以找到配置设置模块/控制器将处理此请求。

谢谢。

1 个答案:

答案 0 :(得分:0)

这是我对Yii2的配置

<强> main.php

    <?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

$host = (!empty($_SERVER['HTTP_HOST']))?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
$segments = explode('.',$host);
defined('SUBDOMAIN') or define('SUBDOMAIN', strtolower($segments[0]));

return [
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        SUBDOMAIN => [
            'class' => 'backend\modules\\'.SUBDOMAIN.'\\'.ucfirst(SUBDOMAIN),
        ],
    ],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-backend',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'advanced-backend',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'db' => [
            'tablePrefix' => SUBDOMAIN.'_',
        ],
        'urlManager'=>[
            'rules' => [
                '<controller:[\w-]+>/<id:\d+>' => ''.SUBDOMAIN.'/<controller>/view',
                '<controller:[\w-]+/<action:[\w-]+>' => ''.SUBDOMAIN.'/<controller>/<action>',
                '<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => ''.SUBDOMAIN.'/<controller>/<action>',
            ],
        ],

    ],
    'params' => $params,
];