Many a times we need use multiple database as per the domain or some other reason . We can easily achieve it with the following code in cakePhp.
Write the following code in app->config->database.php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'firstDatabaseName',
'prefix' => ''
);
public $first = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'firstDatabaseName',
'prefix' => ''
);
public $second= array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'seondDatabaseName',
'prefix' => ''
);
public function __construct() {
$parsed = parse_url($_SERVER['HTTP_HOST']);
$domain = explode('.', $parsed['path']);
$subdomain = '';
if ($domain[0] == 'www')
$subdomain = $domain[1];
else
$subdomain = $domain[0];
if ($subdomain == 'us') {
// use site one database config
$this->default = $this->second;
// elseif site two
}else if($subdomain == 'can'){
$this->default = $this->first;
}
}
}
0 Comment(s)