Environment-based configuration class (for example in development, testing, staging and production).
Using this you can predefine configurations for use in different environments, like development, testing, staging and production.
The main config (main.php) is extended to include the Yii paths and debug flags.
There are mode_.php files for overriding and extending main.php for specific environments.
Additionally, you can overrride the resulting config by using a local.php config, to make
changes that will only apply to your specific installation.
This class was designed to have minimal impact on the default Yii generated files. Minimal changes to the index/bootstrap and existing config files are needed.
The Environment is determined with PHP's getenv(), which searches
There are multiple ways to set the environment depending on your preference.
Setting the environment variable is trivial on both Windows and Linux, instructions included.
You can optionally override the environment by creating a mode.php in the config directory.
If you want to customize this class or its config and modes, extend it (see ExampleEnvironment.php)
Tested on Yii 1.1.5-1.1.14.
Should work on all versions >= 1.0
Usage and details ¶
- 4.0: CHG: Removed function runYiiStatics(). Since Yii 1.1.14, you can now set path aliases in your configWeb. Remove calls to runYiiStatics() from your application, and see example-config/main.php for example on how to use aliases.
- 3.2" REFACTOR: split up setMode(), determine mode in separate function. Also update some docs
- 3.1: ADD: optionally override project environment by creating configDir/mode.php with preferred value
- 3.0: CHG attention: use getenv() to determine environment mode (works for both webapps and console-apps). See updated instructions, it's not always backwards compatible. CHG: refactoring to allow properly extending base class, small cleanup
- 2.5: FIX: incorrectly setting yiic instead of yiit. Made $_mode protected. Reported by glaszig.
- 2.4: FIX: add type checking when comparing INHERIT_KEY to prevent unexpected copying. Reported by mylonov
- 2.3: Separate getConfig() from setEnvironment() to allow extending. Small changes to allow extending.
- 2.2: No functional change: Fixed incorrect config key in example file.
- 2.1: Console application support is now optional, you may omit the 'configConsole' key
- 2.0: Support for console applications! HOW TO UPGRADE: rename key 'config' to 'configWeb' in config files and index files
- 1.4: Allow extending Environment class for overriding and extending constants (SERVER_VAR, CONFIG_DIR, MODE_xxx). Added more examples.
- 1.3: Changed array_replace_recursive() to CMap::mergeArray(), since it wasn't producing expected results. Added showDebug(). Bugfix for PHP<5.3
- 1.2: Initial release