Rodrigo Alexsandro
Rodrigo Alexsandro

Reputation: 11

Yii framework and dynamic subdomains

I want to know how the Yii framework send the request to the right controller depending on the subdomain passed in the URL:

www.mysystem.com -> This request is handled by the default controller of a specific module in my system.

But when the user comes to access their store, they will use the URL: storename.mysystem.com. (There are many different store names)

I would like to know where in Yii can I find the config to set which module/controller will handle this request.

Thank you.

Upvotes: 1

Views: 152

Answers (1)

Alex
Alex

Reputation: 21

Here is my config for 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,
];

Upvotes: 1

Related Questions