2

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

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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_', ); ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.