PHPackages                             gstearmit/envyii2 - 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. gstearmit/envyii2

ActiveLibrary

gstearmit/envyii2
=================

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

047PHP

Since Sep 7Pushed 8y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

PHP env
=======

[](#php-env)

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

Installation with Composer
--------------------------

[](#installation-with-composer)

```
curl -s http://getcomposer.org/installer | php
php composer.phar require  gstearmit/envyii2
```

or composer require "gstearmit/envyii2": "dev-master"

Usage
-----

[](#usage)

The `.env` file is generally kept out of version control since it can contain sensitive API keys and passwords. A separate `.env.example` file is created with all the required environment variables defined except for the sensitive ones, which are either user-supplied for their own development environments or are communicated elsewhere to project collaborators. The project collaborators then independently copy the `.env.example` file to a local `.env` and ensure all the settings are correct for their local environment, filling in the secret keys or providing their own values when necessary. In this usage, the `.env`file should be added to the project's `.gitignore` file so that it will never be committed by collaborators. This usage ensures that no sensitive passwords or API keys will ever be in the version control history so there is less risk of a security breach, and production values will never have to be shared with all project collaborators.

Add your application configuration to a `.env` file in the root of your project. **Make sure the `.env` file is added to your `.gitignore` so it is not checked-in the code**

```
S3_BUCKET="dotenv"
SECRET_KEY="souper_seekret_key"
```

Now create a file named `.env.example` and check this into the project. This should have the ENV variables you need to have set, but the values should either be blank or filled with dummy data. The idea is to let people know what variables are required, but not give them the sensitive production values.

```
S3_BUCKET="devbucket"
SECRET_KEY="abc123"
```

You can then load `.env` in your application with:

```
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
```

Optionally you can pass in a filename as the second parameter, if you would like to use something other than `.env`

```
$dotenv = new Dotenv\Dotenv(__DIR__, 'myconfig');
$dotenv->load();
```

All of the defined variables are now accessible with the `getenv`method, and are available in the `$_ENV` and `$_SERVER` super-globals.

```
$s3_bucket = getenv('S3_BUCKET');
$s3_bucket = $_ENV['S3_BUCKET'];
$s3_bucket = $_SERVER['S3_BUCKET'];
```

You should also be able to access them using your framework's Request class (if you are using a framework).

```
$s3_bucket = $request->env('S3_BUCKET');
$s3_bucket = $request->getEnv('S3_BUCKET');
$s3_bucket = $request->server->get('S3_BUCKET');
$s3_bucket = env('S3_BUCKET');
```

### Nesting Variables

[](#nesting-variables)

It's possible to nest an environment variable within another, useful to cut down on repetition.

This is done by wrapping an existing environment variable in `${…}` e.g.

```
BASE_DIR="/var/webroot/project-root"
CACHE_DIR="${BASE_DIR}/cache"
TMP_DIR="${BASE_DIR}/tmp"
```

### Immutability

[](#immutability)

By default, Dotenv will NOT overwrite existing environment variables that are already set in the environment.

If you want Dotenv to overwrite existing environment variables, use `overload`instead of `load`:

```
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->overload();
```

Requiring Variables to be Set
-----------------------------

[](#requiring-variables-to-be-set)

Using Dotenv, you can require specific ENV vars to be defined, and throw an Exception if they are not. This is particularly useful to let people know any explicit required variables that your app will not work without.

You can use a single string:

```
$dotenv->required('DATABASE_DSN');
```

Or an array of strings:

```
$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
```

If any ENV vars are missing, Dotenv will throw a `RuntimeException` like this:

```
One or more environment variables failed assertions: DATABASE_DSN is missing

```

### Empty Variables

[](#empty-variables)

Beyond simply requiring a variable to be set, you might also need to ensure the variable is not empty:

```
$dotenv->required('DATABASE_DSN')->notEmpty();
```

If the environment variable is empty, you'd get an Exception:

```
One or more environment variables failed assertions: DATABASE_DSN is empty

```

### Integer Variables

[](#integer-variables)

You might also need to ensure the the variable is of an integer value. You may do the following:

```
$dotenv->required('FOO')->isInteger();
```

If the environment variable is not an integer, you'd get an Exception:

```
One or more environment variables failed assertions: FOO is not an integer

```

### Allowed Values

[](#allowed-values)

It is also possible to define a set of values that your environment variable should be. This is especially useful in situations where only a handful of options or drivers are actually supported by your code:

```
$dotenv->required('SESSION_STORE')->allowedValues(['Filesystem', 'Memcached']);
```

If the environment variable wasn't in this list of allowed values, you'd get a similar Exception:

```
One or more environment variables failed assertions: SESSION_STORE is not an
allowed value

```

### Comments

[](#comments)

You can comment your `.env` file using the `#` character. E.g.

```
# this is a comment
VAR="value" # comment
VAR=value # comment
```

```
source .env

```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/153b57763835ec70a99891b09b59be9ae515c9f7fc40e58d614646de569d3c9a?d=identicon)[gstearmit](/maintainers/gstearmit)

---

Top Contributors

[![phuchcws](https://avatars.githubusercontent.com/u/52060767?v=4)](https://github.com/phuchcws "phuchcws (6 commits)")

### Embed Badge

![Health badge](/badges/gstearmit-envyii2/health.svg)

```
[![Health](https://phpackages.com/badges/gstearmit-envyii2/health.svg)](https://phpackages.com/packages/gstearmit-envyii2)
```

PHPackages © 2026

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