Janet Riley

Manage different Yii environment settings with a local config file

November 07, 2014 | In 30 days / tech

I'm maintaining a Yii site where my development, staging, and production environments have different yii_paths and database settings. The project has several contributors. Previously we commented out one batch of settings and uncommenting another, and were extremely careful with commits. It works, but somebody's going to lose an eye.

I don't know what the state of the art is for configuration management in PHP frameworks. Were this deployed on Heroku or a private server, we could keep them in environment variables.

* Make it hard to commit the wrong settings, especially to production
* Make it easy to find the solution - a file in the repository rather than instructions for symlinking
* Keep the local settings optional: it should behave normally if there are no local settings

I extracted the varying settings into a template file. Along with different database connection settings, I use a higher debug setting on my local machine. For extra difficulty points, Yii is installed in a different directory than on production.

Index.php is the entry point for Yii. First, the common protected/config/main.php settings are read in as $base_config . Local overrides, if any, overwrite the values in base_config. The settings are handed off to Yii as usual to run the application.