PHPackages                             morrislaptop/phpdotenv-safe - 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. morrislaptop/phpdotenv-safe

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

morrislaptop/phpdotenv-safe
===========================

Load environment variables from .env and ensure they're all present

v0.0.1(8y ago)380.4k3BSD-3-Clause-AttributionPHP

Since Jul 28Pushed 7y agoCompare

[ Source](https://github.com/morrislaptop/phpdotenv-safe)[ Packagist](https://packagist.org/packages/morrislaptop/phpdotenv-safe)[ RSS](/packages/morrislaptop-phpdotenv-safe/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

morrislaptop/phpdotenv-safe
===========================

[](#morrislaptopphpdotenv-safe)

Drop in replacement for [`vlucas/phpdotenv`](https://github.com/vlucas/phpdotenv), but ensures that all necessary environment variables are defined after reading from .env. These needed variables are read from `.env.example`, which should be committed along with your project.

This is heavily inspired by [dotenv-safe](https://github.com/rolodato/dotenv-safe).

[![Build Status](https://camo.githubusercontent.com/167022f1d20a3f667b9729c2c45d61ec58ee7b1c58a80640f16161e824dd6814/68747470733a2f2f7472617669732d63692e6f72672f6d6f727269736c6170746f702f706870646f74656e762d736166652e737667)](https://travis-ci.org/morrislaptop/phpdotenv-safe)

Installation
============

[](#installation)

```
composer require morrislaptop/phpdotenv-safe

```

Example
=======

[](#example)

```
# .env.example, committed to repo
SECRET=
TOKEN=
KEY=
```

```
# .env, private
SECRET=topsecret
TOKEN=
```

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

Since the provided `.env` file does not contain all the variables defined in `.env.example`, an exception is thrown:

```
PHP Fatal error:  Uncaught Dotenv\Exception\ValidationException: One or more environment variables failed assertions: KEY is missing.

```

Not all the variables have to be defined in `.env`, they can be supplied externally. For example, the following would work:

```
KEY=xyz php index.php

```

Usage with popular frameworks
=============================

[](#usage-with-popular-frameworks)

Laravel
-------

[](#laravel)

Laravel doesn't make it easy to swap out its existing `Dotenv` instance from `vlucas/phpdotenv`, but you can validate the environment manually with `DotenvSafe` when the application boots.

Just add the following to your `AppServiceProvider->register` method (which you'll find in `app/providers/AppServiceProvider.php`):

```
  if (!$this->app->configurationIsCached()) {
      $dotenv = new DotenvSafe\DotenvSafe($this->app->environmentPath(), $this->app->environmentFile());
      $dotenv->check();
  }
```

Lumen
-----

[](#lumen)

`bootstrap/app.php` runs when your application starts, and loads the environment using `Dotenv\Dotenv`. You can easily swap this out to use `DotenvSafe\DotenvSafe`. All you need to do is replace:

```
(new Dotenv\Dotenv(__DIR__.'/../'))->load();
```

with:

```
(new DotenvSafe\DotenvSafe(__DIR__.'/../'))->load();
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

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

Unknown

Total

1

Last Release

3208d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ecf933ed5c8b3e08c1880734166cb32d9e66eb50b01bb91ff74018c3e4b61058?d=identicon)[morrislaptop](/maintainers/morrislaptop)

---

Top Contributors

[![morrislaptop](https://avatars.githubusercontent.com/u/67807?v=4)](https://github.com/morrislaptop "morrislaptop (6 commits)")[![juanisorondo](https://avatars.githubusercontent.com/u/9666184?v=4)](https://github.com/juanisorondo "juanisorondo (1 commits)")

---

Tags

environmentenvdotenv

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/morrislaptop-phpdotenv-safe/health.svg)

```
[![Health](https://phpackages.com/badges/morrislaptop-phpdotenv-safe/health.svg)](https://phpackages.com/packages/morrislaptop-phpdotenv-safe)
```

###  Alternatives

[vlucas/phpdotenv

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

13.5k602.4M5.4k](/packages/vlucas-phpdotenv)[symfony/dotenv

Registers environment variables from a .env file

3.8k226.7M2.2k](/packages/symfony-dotenv)[nystudio107/dotenvy

Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents.

326.9k](/packages/nystudio107-dotenvy)[diarmuidie/envpopulate

Tool to interactively populate a `.env` file based on an `.env.example` file whenever Composer installs or updates.

1892.0k](/packages/diarmuidie-envpopulate)[zepgram/magento-dotenv

Simple autoloader to integrate the Symfony Dotenv component into Magento2

1371.3k](/packages/zepgram-magento-dotenv)

PHPackages © 2026

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