Different configs per domain

Hi Yii’ers,

I would like your opinion on the following:

We are building our Yii app (in the cloud) and have several customers that are going to use this application. They all have their own domain (example: app.company.com, app.anothercompany.com, etc.). We would like to load a different configuration file per domain so we can use a different database connection per domain, a different theme map, etc. We would like to load the config files files from a central place that will be easily to manage.

What would be the best approach in doing this? Any opinions?

Thanks for sharing!

Hi,

Samdark wrote an article about this on his blog. Although my russian skills suck it is easy to get the idea. You simply place a condition in your index.php to load the right config array according to the host

This example loads a config defined in dev.php if the host is localhost, else it is using production.php which it is then merging with the main.php (where you put all the general config stuff into that is used by all domains)




$webRoot=dirname(__FILE__);

 

if($_SERVER['HTTP_HOST']=='localhost'){

    define('YII_DEBUG', true);

    require_once(dirname($webRoot).'/framework/yii.php');

    $configFile=$webRoot.'/../app/config/dev.php';

}

else { 

    define('YII_DEBUG', false);

    require_once(dirname($webRoot).'/framework/yiilite.php');

    $configFile=$webRoot.'/../app/config/production.php';

}

 

$app = Yii::createWebApplication($configFile)->run();



Now merge the arrays in your corresponding configs (dev, production,client1,client2 etc.)


return CMap::mergeArray(

    require(dirname(__FILE__).'/main.php'),

    array(

        'components'=>array(

            'db'=>array(

            ),

        ),

    )

);

Hope it helps!

Cheers,

Hannes

Check this wiki too

doodle

Thanks guys… decided to place the configs in a separate part on our servers and to include based on the domain name. That way we don’t have to create many switch/case statements but just include the ‘/environments/{current-hostname}/config.php’ file.

If there is not a config file for the ‘current’ hostname we will redirect the user to our company website.

Very nice to get good/quick answers in this community! Tnx.

Thank you! This is exactly what I was looking for.

If only I’ve read your post earlier. I’ve just spent a while by making the custom function that does exactly the same as CMap::mergeArray. It’s amazing how many useful components Yii has.