How to use multiple configuration files using Yii

2019-05-27 18:27发布

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

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

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';

标签: php yii
4条回答
Melony?
2楼-- · 2019-05-27 18:31

Try this:

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';
}
查看更多
放我归山
3楼-- · 2019-05-27 18:34

My solution is also based on Yii Framework Separate Configurations for Different Environments. Advantage of this method in fact that common configuration are stored in config/main.php and only differences are stored in config/main_prod.php and config/main_dev.php thanks to CMap::mergeArray.

config/main.php example:

<?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;

Of course, instead of $_SERVER['SERVER_NAME'] you can use 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;
查看更多
做自己的国王
4楼-- · 2019-05-27 18:46

Maybe this article give some information to you.

Yii Framework Separate Configurations for Different Environments

查看更多
我只想做你的唯一
5楼-- · 2019-05-27 18:52

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

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

查看更多
登录 后发表回答