Yii 1.1: yii-environment

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


All information is available within the GitHub-project:

Total 20 comments

#17310 report it
marcovtwout at 2014/05/21 12:03pm
Install through Composer

Since version 5, the preferred installation method is through Composer. See updates instructions https://github.com/marcovtwout/yii-environment/blob/master/README.md

#16688 report it
marcovtwout at 2014/03/19 09:54am
Project moved to GitHub

I moved the project from google code to GitHub, and decided to keep all documentation there. :) See updates description above.

#16394 report it
Accelm at 2014/02/18 03:08pm
RE: How to run from console

Hey LittleBob,

Here in order for me to set up my app to use environments in console I followed the instructions here: http://code.google.com/p/yii-environment/wiki/EnvironmentClass under Setting Environment > Linux and had to set that up for the user who was running the cron job.

Then I had to update my configuration files for the configConsole as that was added in the changelog in V 2.0. The site I linked to up there also should be enough to get you all set there.

#16118 report it
Littlebob at 2014/01/21 12:25pm
How to run from console


Great extension. How do I run console apps using the extension?


#15937 report it
marcovtwout at 2013/12/31 06:17am
New release

I updated the extension with simplified alias creation without using runYiiStatics(). This also means you don't have to copy/include yiic from the framework any more. :)

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

RewriteCond %{SERVER_NAME} ^local-.* [OR]
RewriteCond %{SERVER_NAME} ^dev\..*

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

Leave a comment

Please to leave your comment.

Create extension