How to use multiple configuration files using Yii

I want to use a different configuration file for my development and production server. I want to define a different database configuration for each server and a different logging procedure.

So when I run on my server, I just modify the file index.php.

Development:

// developement
$config=dirname(__FILE__).'/protected/config/development.php';
// production
// $config=dirname(__FILE__).'/protected/config/production.php';

Production:

// developement
// $config=dirname(__FILE__).'/protected/config/development.php';
// production
$config=dirname(__FILE__).'/protected/config/production.php';
+2
source share
4 answers

Perhaps this article will provide you with some information.

Yii Framework Separate configurations for different environments

+7
source

Yii Framework . , config/main.php, config/main_prod.php config/main_dev.php CMap:: mergeArray.

config/main.php:

<?php

$config = array( ... );

switch ($_SERVER['SERVER_NAME']) {
    case 'your-prod-server-name.com':
        $config = CMap::mergeArray(
            $config,
            require(dirname(__FILE__) . '/main_prod.php')
        );
        break;
    default:
        $config = CMap::mergeArray(
            $config,
            require(dirname(__FILE__) . '/main_dev.php')
        );
        break;
}

return $config;

, $_SERVER['SERVER_NAME'] YII_DEBUG:

<?php

$config = array( ... );

if (YII_DEBUG) {
    $config = CMap::mergeArray(
        $config,
        require(dirname(__FILE__) . '/main_dev.php')
    );
} else {
    $config = CMap::mergeArray(
        $config,
        require(dirname(__FILE__) . '/main_prod.php')
    );
}

return $config;
+3

Try the following:

if ($_SERVER['HTTP_HOST'] == 'yourdomain.com') {
    $config = dirname(__FILE__).'/protected/config/production.php';
} else {
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
    $config = dirname(__FILE__).'/protected/config/development.php';
}
0
source

if only change the database connection 'db'=>require($_SERVER['REMOTE_ADDR']=='127.0.0.1' ? 'db_dev.php' : 'db.php'),

and create files in the config directory with the contents <?php return array( 'connectionString' => 'mysql:host=localhost;dbname=yii', 'emulatePrepare' => true, 'schemaCachingDuration' => 3600, 'enableProfiling'=>true, 'enableParamLogging' => true, 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'tablePrefix' => 'tbl_', ); ?>

0
source

All Articles