Simple .env file parser and ENV loader (.env
to getenv()
and $_ENV
) based on standard PHP INI parser (parse_ini_file).
Supported methods:
- getenv(variable)
- getenv(section_variable)
- $_ENV[variable]
- $_ENV[section_variable]
- $dotenv->variable
- $dotenv->section->variable
- $dotenv[variable]
- $dotenv[section][variable]
To install this library, you need to use Composer in your project. If you are not using Composer yet, here's how to install:
curl -sS https://getcomposer.org/installer | php
composer require balint-horvath/dotenv-php
php composer.phar require balint-horvath/dotenv-php
#.env
[API]
apiUser = User
apiKey = Key
Namespace: \BalintHorvath\DotEnv\
Class: DotEnv
new \BalintHorvath\DotEnv\DotEnv($path)
(string)
path: Directory of .env file or full path to your ini file. (default:../../../
)(bool)
setEnvironmentVariables: If it'strue
, variables will be available via environment ($_ENV
,getenv()
), otherwise (iffalse
) they'll be available only via the DotEnv as object or array ($dotenv->
$dotenv[]
). (default:true
)(bool)
processSections: If it'strue
, variables will be organized under sections ($dotenv->section
$dotenv[section]
), otherwise sections will have no matter. (default:true
)(bool)
scannerMode: If it'sINI_SCANNER_TYPED
, values0
/off
/"false"
/false
will becomebool
false
, values1
/on
/"true"
/true
will becomebool
true
. Can either beINI_SCANNER_NORMAL
orINI_SCANNER_RAW
. IfINI_SCANNER_RAW
is supplied, then option values will not be parsed. (See PHP Manual: parse_ini_file and PHP Manual: Predefined Constants for more.) (default:INI_SCANNER_TYPED
)
define('APP_DIR', dirname(__FILE__) . '/');
require 'vendor/autoload.php';
$dotenv = new \BalintHorvath\DotEnv\DotEnv(APP_DIR);
Usage:
$dotenv->{variable}
$dotenv->{section}->{variable}
Example:
$dotenv->API->apiUser
API User: <?=$dotenv->API->apiUser?>
API Key: <?=$dotenv->API->apiKey?>
Usage:
$_ENV['{variable}']
$_ENV['{section}']['{variable}']
Example:
$_ENV['API_apiUser']
API User: <?=$_ENV['API_apiUser']?>
API Key: <?=$_ENV['API_apiKey']?>
Usage:
getenv('variable')
getenv('section_variable')
Example:
getenv('API_apiUser')
API User: <?=getenv('API_apiUser')?>
API Key: <?=getenv('API_apiKey')?>
Usage:
$dotenv[{variable}]
$dotenv[{section}][{variable}]
Example:
$dotenv['API']['apiUser']
API User: <?=$dotenv['API']['apiUser']?>
API Key: <?=$dotenv['API']['apiKey']?>
- Kahlan 4 (kahlan/kahlan:^4.0)
This package has included test cases for Kahlan.
- \BalintHorvath\DotEnv\