Codeigniter:维护多个数据库连接数据的最佳实践是什么? [英] Codeigniter: What is the best practice to maintain multiple database connection datas?
问题描述
我典型的开发模式是:
1 - local work machine
2 - online testing server
3 - production server.
每个数据库都有一个包含唯一连接数据的数据库。
Each of these has a database with unique connection data.
因此对于我的项目,我使用一个配置文件,该文件实现 switch(what_environment){//分配适当的数据库连接信息}
会根据环境分配正确的数据库连接信息(主机,用户名,密码,数据库名称...)。
So for my projects I use a config file that implements a switch( what_environment ){ // assign appropriate db connection info }
that assigns the proper db connection info (host, username, password, db name, ...) depending on the environment.
但是在codeigniter的<$ c中$ c> /config/database.php ,没有这样的构造,而只是一组 属性。显然,我可以在其中放置自己的服务器嗅探switch语句。但是我以为有一种可解决此问题的codeigniter方法。
However in codeigniter's /config/database.php
, there is no such construct, but rather just one set of properties. Obviously I can put my own server-sniffing switch statement in there. But I am presuming that there is a codeigniter way of handling this.
任何人都可以阐明维护多个数据库连接信息的正确codeigniter方法吗?
Can anyone shed light on the proper codeigniter way of maintaining multiple db connection infos?
推荐答案
在您的application / config / constants.php上写:
On your application/config/constants.php write:
define('ENVIRONMENT', 'development');
switch(ENVIRONMENT):
case 'development':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'localhost');
defined('DB_USER') ? null : define('DB_USER', 'root');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '');
defined('DB_NAME') ? null : define('DB_NAME', 'dev_db');
break;
case 'production':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'production');
defined('DB_USER') ? null : define('DB_USER', 'production_user');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '12345');
defined('DB_NAME') ? null : define('DB_NAME', 'production_db');
break;
endswitch;
然后在您的application / config / database.php上
Then on your application/config/database.php
$db['default'] = array(
'hostname' => DB_HOST,
'username' => DB_USER,
'password' => DB_PASSWORD,
'database' => DB_NAME,
);
这篇关于Codeigniter:维护多个数据库连接数据的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!