You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
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):
<?php
$client_config = array(
'connectionString' => 'mysql:host=localhost;dbname=client1',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix'=>'',
);
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
include_once($root.'/clients/'.$client_portal.'.php');
// 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.
How to use it localy ?
Hi Thanks for this great idea
how can this be set up localy when
the SERVER_NAME is 127.0.0.1:8888
should it be with a httpd.conf change ?
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 </Directory> DocumentRoot "/Library/WebServer/Vhosts/yii/simpledental" ServerName sd.local ServerAlias sd.local </VirtualHost>
To use it locally
Create virtual hosts on your dev box.
You need to modify your hosts file.
# 127.0.0.2 example.com 127.0.0.3 nsnet.ca # 127.0.0.4 revolution.com
modify httpd.conf
# Virtual hosts Include conf/extra/httpd-vhosts.conf
add entries in httpd-vhosts.conf
<VirtualHost 127.0.0.3:80> 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 </Directory> </VirtualHost>
Read all about it!
There are probably some steps that I have forgotten.
doodle
Perfect Solution
I have been wondering for a solution to my scenario and reached this page, working like a charm, thanks a lot.
theme and index.php
nice tutorial but can you please post how is index.php (main) and theme folder for it, thanks
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.