yii-environment

Environment-based configuration class (for example in development, testing, staging and production).
44 followers

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

#12893 report it
rooney10 at 2013/04/18 07:35am
Local test file

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 :)

#12721 report it
marcovtwout at 2013/04/08 05:42am
Re: Whats the benefit of a git submodule

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.

#12718 report it
kmindi at 2013/04/08 04:33am
Whats the benefit of a git submodule

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).

#12717 report it
marcovtwout at 2013/04/08 03:20am
Re: for those who want to use as submodule

What do you mean with submodule? The code for this extension is available on Google Code, links are in the description.

#12673 report it
kmindi at 2013/04/04 06:15pm
for those who want to use as submodule

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 ;)).

#12091 report it
marcovtwout at 2013/02/27 03:45am
Re: Thanks + a suggestion

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").

#12087 report it
c@cba at 2013/02/26 06:49pm
Thanks + a suggestion

Many thanks for this extension (thumbs up)!

On the subject:

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.

what would you think of the following approach (any drawbacks?):
- config/main.php stays the same (and mode_development.php, mode_production.php etc. respectively).
- The additional config-parameters for the extension ('yiiPath', 'yiiDebug' etc) go into a separate file env.php (and env_development.php, env_production.php etc. respectively).
- The class Environment gets a new function: getEnvConfig (similar to getConfig)
- The function setEnvironment becomes:

$config = $this->getEnvConfig();
$this->yiiPath = $config['yiiPath'];
if (isset($config['yiicPath']))
    $this->yiicPath = $config['yiicPath'];
if (isset($config['yiitPath']))
    $this->yiitPath = $config['yiitPath'];
$this->yiiDebug = $config['yiiDebug'];
$this->yiiTraceLevel = $config['yiiTraceLevel'];
 
require_once($this->yiiPath); // <---- Here we include the Yii core class
$this->configWeb = $this->getConfig();
$this->configWeb['params']['environment'] = strtolower($this->mode);
 
$this->configConsole = $this->getConsoleConfig();
if(!empty($this->configConsole)) {
    $this->processInherits($this->configConsole); // Process configConsole for inherits
    $this->configConsole['params']['environment'] = strtolower($this->mode);
}
$this->yiiSetPathOfAlias = $env_config['yiiSetPathOfAlias'];
#11051 report it
sidewinder at 2012/12/12 09:13am
Determining environment based on domain name

@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.'

#11050 report it
marcovtwout at 2012/12/12 08:56am
Re: Re: Re: Prevent Problems with local.php

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! :)

#11049 report it
MatiH at 2012/12/12 08:45am
Re: Re: Prevent Problems with local.php

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'...)

#11048 report it
marcovtwout at 2012/12/12 08:21am
Re: Prevent Problems with local.php

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. ;)

#11047 report it
MatiH at 2012/12/12 08:13am
Prevent Problems with local.php

To prevent problems with a mistake upload of local.php to the server, I added in beginning of the file:

if($_SERVER['HTTP_HOST'] != 'domain.local')
    return array();

But I think it needs another method to include a local file, something like - include a file with current domain: domain.php:

$fileLocalConfig = $this->getConfigDir().$_SERVER['HTTP_HOST'].'.php';
if (file_exists($fileLocalConfig )) {...
#10183 report it
rooney10 at 2012/10/09 07:01am
Re: Re: Question to test environment and local.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 :)

#10176 report it
marcovtwout at 2012/10/09 05:09am
Re: Question to test environment and local.php

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

#10174 report it
rooney10 at 2012/10/09 04:56am
Question to test environment and local.php

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 :)

#9360 report it
riemann at 2012/08/06 11:35pm
Thabks

Very helpfull extension

#8923 report it
PrplHaz4 at 2012/07/07 07:11pm
Thanks for the great 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.

#8859 report it
marcovtwout at 2012/07/04 12:35pm
Re: CClientScript not found

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:

'clientScript' => array(
    'coreScriptPosition'=>2, //CClientScript::POS_END in Yii 1.x
)},
#8858 report it
barcelona23 at 2012/07/04 10:29am
CClientScript not found [SOLVE]

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

'clientScript' => array(
    'coreScriptPosition'=>CClientScript::POS_END,
)},

Any help??

#8684 report it
fr0d0z at 2012/06/19 09:58am
Regarding Yii versions

I'm using it on multiple sites with 1.1.11

Leave a comment

Please to leave your comment.

Create extension