PHPackages                             marcovtwout/yii-environment - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. marcovtwout/yii-environment

ActiveLibrary

marcovtwout/yii-environment
===========================

Environment class for Yii, used to set configuration for console and web apps depending on the server environment.

5.0.1(12y ago)29196.3k—3.8%11BSD-3-ClausePHPPHP &gt;=5.3

Since May 21Pushed 9y ago7 watchersCompare

[ Source](https://github.com/marcovtwout/yii-environment)[ Packagist](https://packagist.org/packages/marcovtwout/yii-environment)[ Docs](https://github.com/marcovtwout/yii-environment)[ RSS](/packages/marcovtwout-yii-environment/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

yii-environment
===============

[](#yii-environment)

[![Latest Stable Version](https://camo.githubusercontent.com/13c48bd798585576cb5d98d765296360ff5ab18da99d2ba0b13f02960b81c3cf/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6172636f7674776f75742f7969692d656e7669726f6e6d656e742e7376673f7374796c653d666c6174)](https://packagist.org/packages/marcovtwout/yii-environment)[![Total Downloads](https://camo.githubusercontent.com/474f82c11f851d6a68945caa56f127faa900f061f90456ce5842af97532d08b6/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6172636f7674776f75742f7969692d656e7669726f6e6d656e742e7376673f7374796c653d666c6174)](https://packagist.org/packages/marcovtwout/yii-environment)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/6a5b6be9b3be041a2d1b6d4d6a65bab6e989fdc5d23dc995ccaf5d0cfdf2e2e3/687474703a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d6172636f7674776f75742f7969692d656e7669726f6e6d656e742e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/marcovtwout/yii-environment/)

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

Simple class used to set configuration and debugging depending on environment. 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 on a per-project basis.

If you want to customize this class or its config and modes, extend it! (see [ExampleEnvironment.php](ExampleEnvironment.php))

Requirements
------------

[](#requirements)

Yii 1.1.\*

Installation
------------

[](#installation)

### Installation via Composer

[](#installation-via-composer)

Since version 5 the preferred installation method is through [Composer](https://getcomposer.org/). We assume the vendor directory is placed in `protected/vendor`.

1. Add the dependency to your project

    ```
    php composer.phar require marcovtwout/yii-environment "5.*"

    ```
2. Modify your `index.php` (and other bootstrap files)
3. Modify your `main.php` config file and add mode specific config files
4. Set your local environment (see next section)

### Installation from zip file

[](#installation-from-zip-file)

See the previous instructions:

### Setting environment

[](#setting-environment)

Here are some examples for setting your environment to `DEVELOPMENT`.

#### Windows

[](#windows)

1. Go to: Control Panel &gt; System &gt; Advanced &gt; Environment Variables
2. Add new SYSTEM variable: name = `YII_ENVIRONMENT`, value = `DEVELOPMENT`

- Details:

#### Linux/Mac

[](#linuxmac)

1. Open profile files:

- Global bash shell: `/etc/profile`
- Apache (as service): `/etc/apache2/envvars`

2. Add the following line: `export YII_ENVIRONMENT="DEVELOPMENT"`

- Details: [http://www.cyberciti.biz/faq/linux-unix-set-java\_home-path-variable/](http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/)

#### Apache only (cannot be used for console applications)

[](#apache-only-cannot-be-used-for-console-applications)

1. Check if mod\_env is enabled
2. Open your `httpd.conf` or create a `.htaccess` file
3. Add the following line: `SetEnv YII_ENVIRONMENT DEVELOPMENT`

- Details: [http://httpd.apache.org/docs/1.3/mod/mod\_env.html#setenv](http://httpd.apache.org/docs/1.3/mod/mod_env.html#setenv)

#### Project only

[](#project-only)

1. Create a file `mode.php` in the config directory of your application.
2. Set the content of the file to: `DEVELOPMENT`

Usage
-----

[](#usage)

### Update bootstrap files

[](#update-bootstrap-files)

See [example-index/index.php](example-index/index.php)

#### Override environment

[](#override-environment)

You can override the automatic determination of the current environment if needed:

- Pass the environment to the constructor: `new Environment('TEST')`
- For console applications, you can also do: `YII_ENVIRONMENT=STAGING ./yiic`

### Structure of config directory

[](#structure-of-config-directory)

Your `protected/config/` directory will look like this:

```
config/main.php                     (Global configuration)
config/mode_development.php         (Environment-specific configurations)
config/mode_test.php
config/mode_staging.php
config/mode_production.php
config/local.php                    (Optional, local override for mode-specific config. Don't put in your SVN!)

```

### Modify your config/main.php

[](#modify-your-configmainphp)

See [example-config/main.php](example-config/main.php)

Optional: in configConsole you can copy settings from configWeb by using value key `inherit` (see examples folder).

### Create mode-specific config files

[](#create-mode-specific-config-files)

Create `config/mode_.php` files for the different modes. These will override or merge attributes that exist in the main config.

- See [example-config/mode\_development.php](example-config/mode_development.php)
- See [example-config/mode\_test.php](example-config/mode_test.php)
- See [example-config/mode\_staging.php](example-config/mode_staging.php)
- See [example-config/mode\_production.php](example-config/mode_production.php)

Optional: also create a `config/local.php` file for local overrides.

Credits
-------

[](#credits)

Original sources:

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

4380d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7c1635dd8638c04f84d60d74d820605821f413df6efca9eb0bfd9d5389246d25?d=identicon)[marcovtwout](/maintainers/marcovtwout)

---

Top Contributors

[![marcovtwout](https://avatars.githubusercontent.com/u/438046?v=4)](https://github.com/marcovtwout "marcovtwout (41 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

environmentyii

### Embed Badge

![Health badge](/badges/marcovtwout-yii-environment/health.svg)

```
[![Health](https://phpackages.com/badges/marcovtwout-yii-environment/health.svg)](https://phpackages.com/packages/marcovtwout-yii-environment)
```

###  Alternatives

[vlucas/phpdotenv

Loads environment variables from `.env` to `getenv()`, `$\_ENV` and `$\_SERVER` automagically.

13.5k602.4M5.4k](/packages/vlucas-phpdotenv)[sebastian/environment

Provides functionality to handle HHVM/PHP environments

6.8k871.7M66](/packages/sebastian-environment)[symfony/dotenv

Registers environment variables from a .env file

3.8k226.7M2.3k](/packages/symfony-dotenv)[raoul2000/yii-simple-workflow

A simple workflow engine for Yii 1

278.2k](/packages/raoul2000-yii-simple-workflow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
