PHPackages                             xapon/yii2-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. xapon/yii2-environment

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

xapon/yii2-environment
======================

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

v1.0.1(11y ago)44.6k4[1 issues](https://github.com/xapon/yii2-environment/issues)BSD-3-ClausePHPPHP &gt;=5.4

Since Jul 28Pushed 10y ago2 watchersCompare

[ Source](https://github.com/xapon/yii2-environment)[ Packagist](https://packagist.org/packages/xapon/yii2-environment)[ Docs](https://github.com/xapon/yii2-environment)[ RSS](/packages/xapon-yii2-environment/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

yii2-environment [![Latest Stable Version](https://camo.githubusercontent.com/9329583f565e078ccb5051e03de320530f44808363c32363cc8256c528f29e56/68747470733a2f2f706f7365722e707567782e6f72672f7861706f6e2f796969322d656e7669726f6e6d656e742f762f737461626c652e737667)](https://packagist.org/packages/xapon/yii2-environment)
============================================================================================================================================================================================================================================================================================================================

[](#yii2-environment-)

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

It is used to set configuration and debugging depending on environment. You can predefine configurations for use in different environments, like *development, testing, staging and production*.

This library is mainly based on [Environment extension for Yii 1](https://github.com/marcovtwout/yii-environment) by [Marco van 't Wout](https://github.com/marcovtwout). Those who have accustomed with it's previous version could easily use this version with Yii2. It is probably useless for `advanced application template` since environment management for it has already been provided by framework developers. Nevertheless, I don't like advanced template for several reasons but do want to handle environment configurations in basic template.

The main config (`main.php`) is extended to include the Yii path 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](ExampleEnvironment.php))

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

[](#requirements)

Tested on current (unstable) Yii2, and will probably work after final release (I'll keep track on it's updates to be sure).

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

[](#installation)

### Installation via Composer

[](#installation-via-composer)

The preferred installation method is through [Composer](https://getcomposer.org/). We assume the vendor directory is placed in application root.

1. Add the dependency to your project

    ```
    php composer.phar require xapon/yii2-environment "1.*"

    ```
2. Modify your `web/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)

Download [zip archive](https://github.com/xapon/yii2-environment/archive/master.zip) and extract it to, say, `components` directory. Then include the Environment class manually in your index.php since composer autoload will not be available for it.

### Setting environment

[](#setting-environment)

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

#### Windows

[](#windows)

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

- 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_ENV="DEV"`

- 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_ENV DEV`

- 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: `DEV`

Usage
-----

[](#usage)

### Update bootstrap files

[](#update-bootstrap-files)

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

### Structure of config directory

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

Your `config/` directory will look like this:

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

```

### 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 `@` (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\_dev.php](example-config/mode_dev.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\_prod.php](example-config/mode_prod.php)

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

Credits
-------

[](#credits)

Original library for Yii 1:

Notes
-----

[](#notes)

- I tried to keep the library as much compatible with it's previous version as possible. Still, I have introduced some changes.

    - First, in order to 'inherit' configuration from `configWeb` in `configConsole` you should use `@` symbol instead of `inherit` keyword (that looks much shorter!).
    - Second, in order to keep the names of environments uniformly with Yii2, I have changed `development` mode to `dev`, `production` to `prod`, and `testing` to `test`. There're now handy boolean constants like `YII_ENV_DEV` set up for you by Yii, and this library makes sure they're defined according to selected `mode`.
- This is probably my first project on github, so I gladly expect issue reports, pull requests, any help, comments or criticism.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.4% 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 ~170 days

Total

2

Last Release

4136d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/14db56f6656a36389bccbb44d7c5655a2669a210bc4babf992a7e40553bec236?d=identicon)[xapon](/maintainers/xapon)

---

Top Contributors

[![marcovtwout](https://avatars.githubusercontent.com/u/438046?v=4)](https://github.com/marcovtwout "marcovtwout (32 commits)")[![antonsergeyev](https://avatars.githubusercontent.com/u/98897?v=4)](https://github.com/antonsergeyev "antonsergeyev (1 commits)")[![lenarm](https://avatars.githubusercontent.com/u/3997744?v=4)](https://github.com/lenarm "lenarm (1 commits)")[![PrplHaz4](https://avatars.githubusercontent.com/u/841414?v=4)](https://github.com/PrplHaz4 "PrplHaz4 (1 commits)")

---

Tags

environmentyii2yii

### Embed Badge

![Health badge](/badges/xapon-yii2-environment/health.svg)

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

###  Alternatives

[kop/yii2-scroll-pager

Infinite AJAX scrolling for Yii2 ListView widget

180706.5k10](/packages/kop-yii2-scroll-pager)[brussens/yii2-maintenance-mode

Maintenance mode component for Yii framework 2.x.x version.

78256.3k5](/packages/brussens-yii2-maintenance-mode)

PHPackages © 2026

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