codemonster-ru / env
Lightweight .env loader for PHP projects
Installs: 51
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/codemonster-ru/env
Requires
- php: >=8.2
Requires (Dev)
- phpunit/phpunit: ^9.6 || ^10.5 || ^11.0 || ^12.0
README
A simple and lightweight .env loader for PHP projects.
๐ฆ Installation
Via Composer:
composer require codemonster-ru/env
๐ Usage
Create a .env file in the root of your project:
APP_NAME=MyApp FEATURE_ENABLED=true FEATURE_DISABLED=false OPTIONAL_VALUE=null EMPTY_VALUE=empty SSR_URL="http://localhost:3000"
Load .env in your app:
<?php require __DIR__ . '/vendor/autoload.php'; use Codemonster\Env\Env; // Load environment variables from .env Env::load(__DIR__ . '/.env'); // Access values via Env::get() echo Env::get('APP_NAME'); // "MyApp" var_dump(Env::get('FEATURE_ENABLED')); // true (bool) var_dump(Env::get('FEATURE_DISABLED')); // false (bool) var_dump(Env::get('OPTIONAL_VALUE')); // null var_dump(Env::get('EMPTY_VALUE')); // "" echo Env::get('SSR_URL'); // http://localhost:3000 echo Env::get('NOT_DEFINED', 'default'); // "default"
โจ Features
- Loading
.envfiles into$_ENV,$_SERVER, and viaputenv(). - Boolean value support:
true,(true)โtruefalse,(false)โfalse- Support for
nullandempty: null,(null)โnullempty,(empty)โ""- Support for quoted strings
"..."and'...'. - Global function
env($key, $default = null).
๐งช Testing
You can run tests with the command:
composer test