2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[baserCMS]DB設定ファイルを環境別に用意しなくて済む方法

Last updated at Posted at 2014-07-05

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の場合とかでも使えるんじゃないでしょかね。

参考情報

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?