Codeigniter: What is the best practice to maintain

2019-07-15 05:30发布

问题:

My typical development pattern is:

1 - local work machine 

2 - online testing server 

3 - production server. 

Each of these has a database with unique connection data.

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.

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.

Can anyone shed light on the proper codeigniter way of maintaining multiple db connection infos?

回答1:

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;

Then on your application/config/database.php

$db['default'] = array(

    'hostname' => DB_HOST,
    'username' => DB_USER,
    'password' => DB_PASSWORD,
    'database' => DB_NAME,

);


回答2:

I do not know if this is the best solution, however, it is the simplest to implement and always worked very well for me:

After the declaration of $db['default']:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '...',
    'password' => '...',
    ...
);

I add this code:

if (ENVIRONMENT == 'development'){
    $db['default']['username'] = 'root';
    $db['default']['password'] = 'XXXXX';
    $db['default']['database'] = 'developmentdatabasename';
}

In the index.php of your project:

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');


回答3:

if you use apache - activate SetEnvIf (if it itsn't already)

after that put in your htaccess

SetEnvIf Host localhost$ CI_ENV=development
SetEnvIf Host testhost$ CI_ENV=testing
SetEnvIf Host productionhost$ CI_ENV=production

now just simply create in your application/config/ folder 3 folder named development, testing and production

and if you put now your different config files in this folders - ci accesses it depending on your current working environment

please follow the CI Documentation for further Information



回答4:

At your config/database.php you can set multiple environment like this

$active_group = 'local';
if(ENVIRONMENT=='development')
{
   $active_group = 'online';
}
elseif(ENVIRONMENT=='production')
{
   $active_group = 'production';
}
$db['local'] = array(
'dsn'   => '',
'hostname' => 'xxxx',
'username' => '',
//other configs
);
$db['online'] = array(
'dsn'   => '',
//other configs
);
$db['production'] = array(
'dsn'   => '',
//other configs
);

You can do it lots of way.See documentation