Yii 1.1: Sub-domains with different databases in Yii!


Sub-domains with different databases in Yii!

So my goal today is to explain how you can have the same core code powering multiple portals (sub-domains) without all the extra config fluff.

I used another example posted here a while back where the person was modifying the index.php and adding in a switch case for different domains and loading separate config files. This was actually too much for me, because all my portals are the exact same (as far as routes, modules, extensions, etc) and if I ever wanted to add another route it wouldn't be feasible opening up all the config files to do so. So the only thing that differs between my clients portals is in fact the database connection.

So let's get started:

I have a project where I give each client access to a their "portal" via say: client1.simpledentalapp.com and one thing about is it they each need a separate database for security.

The set up

Create a folder within your protected directory called clients (or whatever you want to call it).

Create a new file for each subdomain with different database credentials.

Each file should look like this (example: client1.php):

$client_config = array(
            'connectionString' => 'mysql:host=localhost;dbname=client1',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',

The code

So inside of config/main.php I added the following code and changed the return array area to $config_array = array (...);

// remember, above this we have the entire config set to $config_array = array(...)
$exp = explode(".",$_SERVER['SERVER_NAME']);
$client_portal = trim(strtolower($exp[0]));
$root = dirname(__FILE__);
//remove trailing config dir, messy but hey...
$root = str_replace("/config", "", $root);
if(!file_exists($root.'/clients/'.$client_portal.'.php')) {
    die("Sorry, this client portal <strong>".$client_portal."</strong> is not set up or is invalid.");
// include db
// overwrite the db connection info from our included $client_config;
$config_array['components']['db'] = $client_config;
return $config_array;

And that's it. Pretty simple and a lot less headache when trying to manage just a simple database connection and nothing more.

Hope this helps someone. By the way, the example is using MySQL but Postgres and SQLite, etc will all work with this, the connection array just looks a little different.

Total 5 comments

#19917 report it
sefburhan at 2016/07/28 11:48am
theme and index.php

nice tutorial but can you please post how is index.php (main) and theme folder for it, thanks

#17464 report it
deeptibaghel at 2014/06/17 07:17am
Perfect Solution

I have been wondering for a solution to my scenario and reached this page, working like a charm, thanks a lot.

#4131 report it
got 2 doodle at 2011/06/08 10:24am
To use it locally

Create virtual hosts on your dev box. You need to modify your hosts file.

# example.com nsnet.ca
# revolution.com

modify httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

add entries in httpd-vhosts.conf

    ServerAdmin webmaster@dummy-host.localhost
    DocumentRoot "C:/Users/jbenett/Documents/site-source/nsnet.ca"
    ServerName nsnet.ca
    ServerAlias www.nsnet.ca
    ErrorLog "logs/nsnet.ca.log"
    CustomLog "logs/nsnet.ca.log" common
    <Directory />
        Options FollowSymLinks
        AllowOverride all
        Order deny,allow
        Allow from all

Read all about it!

There are probably some steps that I have forgotten. doodle

#4129 report it
jwerd at 2011/06/08 10:19am
That should work, here's how I'm doing it :)

Yep, that should do it. Here's how my vhost entry looks:

<VirtualHost *:80>
        <Directory /Library/WebServer/Vhosts/yii/simpledental>
                AllowOverride All
                allow from all
        DocumentRoot "/Library/WebServer/Vhosts/yii/simpledental"
        ServerName sd.local
        ServerAlias sd.local
#4120 report it
Tibor Katelbach at 2011/06/08 02:57am
How to use it localy ?

Hi Thanks for this great idea how can this be set up localy when the SERVER_NAME is should it be with a httpd.conf change ?

Leave a comment

Please to leave your comment.

Write new article