dotenv-php

dotenv

Simple .env file parser and ENV loader (.env to getenv() and $_ENV) based on standard PHP INI parser (parse_ini_file).

Supported methods:

Examples

Installation

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

via Composer

composer require balint-horvath/dotenv-php

Phar

php composer.phar require balint-horvath/dotenv-php

Example Environment File (INI) (.env)

#.env
[API]
apiUser = User
apiKey = Key

Usage (Instance)

Class

Namespace: \BalintHorvath\DotEnv\

Class: DotEnv

new \BalintHorvath\DotEnv\DotEnv($path)

Properties

Example


define('APP_DIR', dirname(__FILE__) . '/');

require 'vendor/autoload.php';

$dotenv = new \BalintHorvath\DotEnv\DotEnv(APP_DIR);

Getting environment variables

Object Access

Usage:

    $dotenv->{variable}
    $dotenv->{section}->{variable}

Example:

    $dotenv->API->apiUser
    API User: <?=$dotenv->API->apiUser?>
    API Key: <?=$dotenv->API->apiKey?>

ENV Access ($_ENV)

Usage:

    $_ENV['{variable}']
    $_ENV['{section}']['{variable}']

Example:

    $_ENV['API_apiUser']
    API User: <?=$_ENV['API_apiUser']?>
    API Key: <?=$_ENV['API_apiKey']?>

ENV Access (getenv)

Usage:

    getenv('variable')
    getenv('section_variable')

Example:

    getenv('API_apiUser')
    API User: <?=getenv('API_apiUser')?>
    API Key: <?=getenv('API_apiKey')?>

Array Access

Usage:

    $dotenv[{variable}]
    $dotenv[{section}][{variable}]

Example:

    $dotenv['API']['apiUser']
    API User: <?=$dotenv['API']['apiUser']?>
    API Key: <?=$dotenv['API']['apiKey']?>

Dependencies

Developer Dependencies

Unit & BDD Test

This package has included test cases for Kahlan.

PSR

PSR-4 Autoload