baserCMSのインストール時に生成される database.php にはDBへの接続設定が記載されます。
環境別にファイルを用意しても良いけれども、どうせなら git や subversion に入れちゃってひとつのファイルで済ませたい!という方のためへ。
サーバ環境から判断して接続内容を切り替えさせる
サーバの「HTTP_HOST」の値で設定内容を書換えるやり方があります。
本番環境への影響を少なくするため
デフォルトの接続情報を本番環境用とし、開発環境、ステージング環境(デモ環境)は個別に切り分けるように記述すると良いです。
database.php
<?php
//
// Database Configuration File created by baserCMS Installation
//
class DATABASE_CONFIG {
public $baser = array(
'datasource' => 'Database/BcMysql',
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'login' => 'ACCOUNT',
'password' => 'PASSWORD',
'database' => 'DB_NAME',
'schema' => '',
'prefix' => 'mysite_',
'encoding' => 'utf8'
);
public $plugin = array(
'datasource' => 'Database/BcMysql',
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'login' => 'ACCOUNT',
'password' => 'PASSWORD',
'database' => 'DB_NAME',
'schema' => '',
'prefix' => 'mysite_pg_',
'encoding' => 'utf8'
);
public function __construct() {
// Staging
if (strpos($_SERVER['HTTP_HOST'], '.STG_HOST_NAME') !== false) {
$this->baser['host'] = 'localhost';
$this->baser['login'] = 'ACCOUNT';
$this->baser['password'] = 'PASSWORD';
$this->baser['database'] = 'DB_NAME';
$this->plugin['host'] = 'localhost';
$this->plugin['login'] = 'ACCOUNT';
$this->plugin['password'] = 'PASSWORD';
$this->plugin['database'] = 'DB_NAME';
}
// Develop
if (strpos($_SERVER['HTTP_HOST'], '.DEV_HOST_NAME') !== false) {
$this->baser['host'] = 'localhost';
$this->baser['login'] = 'ACCOUNT';
$this->baser['password'] = 'PASSWORD';
$this->baser['database'] = 'DB_NAME';
$this->plugin['host'] = 'localhost';
$this->plugin['login'] = 'ACCOUNT';
$this->plugin['password'] = 'PASSWORD';
$this->plugin['database'] = 'DB_NAME';
}
}
}
HTTP_HOSTで分岐させることで幾つも設定を持たせておくことができますね。
HTTP_HOST自体は phpinfo() とかで確認すると良いです。
SQLiteの場合とかでも使えるんじゃないでしょかね。