train
train

Reputation: 610

Yii parameterizing hostnames doesn't work with subdomains

I am following this "guide" here and I am trying to get Yii to parse the subdomain from an url. However, this doesn't seem to work. Here is what I have in my urlManager in my main config file:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName' => false,
    'rules'=>array(
        'login' => 'site/login',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        '<subdomain:\w+>.lvho.st' => '<controller>/<action>',        <--this one
    ),
),

I am using "lvho.st" to access my localhost (I am using xampp) as some other people at stackoverflow have suggested it to test out subdomains on localhost without messing with the host file. However, when I try to access the "subdomain" by using:

$subdomain = $_GET['subdomain'];

PHP errors out and complains that the subdomain get parameter doesn't exist. The exact error message I am getting:

Undefined index: subdomain

I have also tried enabling vhosts in apache and adding localhost to the windows host file, but that still doesn't work. Does vhosts have to be enabled for Yii to do this? The "guide" I refered to above does not mention needing vhost to be enabled.

Upvotes: 0

Views: 453

Answers (2)

Let me see
Let me see

Reputation: 5094

change your url to this

'http://<subdomain:\w+>.lvho.st/<controller:\w+>/<action:\w+>' => '<controller>/<action>',

and in the corresponding action of the controller do not forget to set a parameter with name subdomain like

public function actionWhatever($subdomain)
{

echo $subdomain;
}

Upvotes: 1

acorncom
acorncom

Reputation: 5955

Handling parametrized sub domains is a bit tricky. You'll need to add in your protocol first (http:// or https://).

What gets tricky is if you need to support SSL on your main production system and non-SSL routes. If that is the case, you'll either need a lot of duplicate routes or to add the protocol on using a UrlManager class override

Upvotes: 1

Related Questions