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 $_SERVER and $_ENV.
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)
Requirements ¶
Tested on Yii 1.1.5-1.1.12.
Should work on all versions >= 1.0
Usage and details ¶
See: http://code.google.com/p/yii-environment/wiki/EnvironmentClass
Resources ¶
Changelog ¶
- 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
Total 20 comments
Hey guys,
what do you think is the best way to have a local test configuration. I already created a local development file, which works without problems. But for testing, i need to specify a different database and I cannot use mode_test.php since that file is for the test system.
Thx in advance :)
I use both, but in my environment Subversion is still used most. Thank you for explaining the extra benefits, I will consider moving my google code projects to github.
First: I use git for version control (which is superior to svn in most use cases) Second: A git submodule is a seperate repository within a "parent"-git repository. It tracks the changes by itself. Wheras the "parent" repository only stores the id of the commit of the submodules.
The benefit of this is, that I/one does not have to track the changes in my own repositories, which would be redundant, because the code is already tracked in your/the repository of that library/plugin/tool/extension.
Therefore I recommend you switch from svn to git (which provides linear history just like svn does).
What do you mean with submodule? The code for this extension is available on Google Code, links are in the description.
I found a git repo which is up to date (currently) https://github.com/flamusdiu/yii-environment
I would definetly like the original author to put the code himself in a puplicly available git repository (for example github ;)).
First of all, thanks for your feedback. :)
Your suggestion would certainly work for what you want to achieve. My personal preference: I like having just one file per environment which contains all needed configuration. It keeps it straightforward (prevent naming ambiguity, what does envConfig mean versus config?). In case I need to use a Yii class constant in one of my configuration files, I simply copy it's value and comment the const name, which is acceptable for me (example in comment "Re: CClientScript not found").
Many thanks for this extension (thumbs up)!
On the subject:
what would you think of the following approach (any drawbacks?):
-
config/main.phpstays the same (andmode_development.php,mode_production.phpetc. respectively).- The additional config-parameters for the extension (
'yiiPath', 'yiiDebug'etc) go into a separate fileenv.php(andenv_development.php,env_production.phpetc. respectively).- The class
Environmentgets a new function:getEnvConfig(similar togetConfig)- The function
setEnvironmentbecomes:@MatiH You could easily set correct environment in a .htaccess file. Example:
RewriteRule ^ - [E=YII_ENVIRONMENT:PRODUCTION] RewriteCond %{SERVER_NAME} ^local-.* [OR] RewriteCond %{SERVER_NAME} ^dev\..* RewriteRule ^ - [E=YII_ENVIRONMENT:DEVELOPMENT]Above snippet will set YII_ENVIRONMENT to PRODUCTION by default and to DEVELOPMENT when server name begins with 'local-' or 'dev.'
If I understand correctly, you would change the name of the config file depending on the domain set in the webserver? By setting an environment variable, you only have to do this once no matter how many applications you have installed. Also, determining domain would only work combined with the web server, but this extension is also fit for command line use. Suggestions are always welcome though! :)
You are right (I use sftp only between the local dev and remote dev), But you don't think it's more convenient to use a config file per domain, dev.domain, test.domain, domain.local and etc...?
(instead of using 'Environment Variables'...)
You won't accidentally upload the file if you're using a versioning system like SVN or Git, and this will be true for most people. If you're updating your website just with FTP, you got more problems to worry about ;)
For edge cases like your suggestion, feel free to extend the class and customize it for your own use. ;)
To prevent problems with a mistake upload of local.php to the server, I added in beginning of the file:
But I think it needs another method to include a local file, something like - include a file with current domain:
domain.php:Thx for the fast response! I did as you said and extended the Environment.php file to serve an environment 'mode_development_local'. Furthermore I will exclude this file from my repository, so that everybody can configure this file on his own!
Thx again :)
Thanks for your feedback :)
If your local.php sets parameters that you don't want to use with mode_test.php, you could: - Move those settings to mode_development.php. - Extend to create a new environment that holds your local changes, ie: mode_development_local (instructions in wiki) - Do a second checkout of your project
First of all I want to point out that this is a great extension and also the documentation is very detailed and useful.
To my question: I'm currently setting up this extension as it is recommended in the WIKI. Nevertheless I have the following situation: I use the file 'local.php' to run the application on my local machine, but unfortunately this makes testing on this machine impossible, because the config in 'local.php' overrides the test config.
How could I manage to use a 'local.php' file to run it on my local machine AND also test on my local machine?
Did I miss something? Thx in advance :)
Very helpfull extension
I was a little leery about undertaking this one as I'm still new to Yii AND Git, but it worked out great with very little effort. Using this extension in conjunction with git, i can deploy through QA and Prod with a couple clicks each. This functionality should definitely be part of the core.
For anyone curious, here is the article I used to set up my deployments: http://toroid.org/ams/git-website-howto
I'm using NetBeans with the built-in git module, and it is literally save, commit, push to a live site.
Because the Yii path is part of the environment configuration, it is included in that specific config file. That means that the Yii core classes will be included AFTER the configuration file has been parsed. Which means CClientScript does not yet exist when you create the configuration. This is a limitation of the current implementation.
In your case, I would simply solve it like this:
Great extension!!! But I am having some problem with it,
I get this error when I defined the coreScriptPotision in my main.php
Fatal error: Class 'CClientScript' not found in C:\xampp\htdocs\yii-locations\protected\config\main.php on line 78
Any help??
I'm using it on multiple sites with 1.1.11
Leave a comment
Please login to leave your comment.