julienlinard/php-dotenv

Une librairie PHP simple et moderne pour charger les variables d'environnement depuis un fichier .env

Installs: 173

Dependents: 1

Suggesters: 0

Security: 0

Stars: 11

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/julienlinard/php-dotenv

1.0.2 2025-11-17 10:44 UTC

This package is auto-updated.

Last update: 2025-11-23 15:51:51 UTC


README

๐Ÿ‡ซ๐Ÿ‡ท Read in French | ๐Ÿ‡ฌ๐Ÿ‡ง Read in English

๐Ÿ’ Support the project

If this bundle is useful to you, consider becoming a sponsor to support the development and maintenance of this open source project.

A simple and modern PHP library for loading environment variables from a .env file.

๐Ÿš€ Installation

composer require julienlinard/php-dotenv

Requirements: PHP 8.0 or higher

โšก Quick Start

<?php

require_once __DIR__ . '/vendor/autoload.php';

use JulienLinard\Dotenv\Dotenv;

// Load the .env file from the root directory
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Access variables
echo $_ENV['DB_HOST'];
echo $_ENV['DB_NAME'];

๐Ÿ“‹ Features

  • โœ… .env file loading
  • โœ… Comment support (lines starting with #)
  • โœ… Support for single and double quoted values
  • โœ… Multi-line value support
  • โœ… Variable expansion (${VAR} or $VAR)
  • โœ… Immutable mode (does not replace existing variables)
  • โœ… Required variable validation
  • โœ… Boolean and null value support

๐Ÿ“– Usage

Basic Loading

use JulienLinard\Dotenv\Dotenv;

// Create an immutable instance (does not replace existing variables)
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

Mutable Loading

// Create a mutable instance (replaces existing variables)
$dotenv = Dotenv::createMutable(__DIR__);
$dotenv->load();

Required Variable Validation

use JulienLinard\Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Validate that certain variables exist
$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);

Validation with Default Values

// Validate with default values
$dotenv->required(['DB_PORT'])->notEmpty()->defaultTo('3306');

Direct Variable Retrieval

// Get a variable with default value
$dbHost = Dotenv::get('DB_HOST', 'localhost');

๐Ÿ“ .env File Format

# Comment
DB_HOST=localhost
DB_NAME=mydatabase
DB_USER=root
DB_PASS=password123

# Quoted values
APP_NAME="My Application"
APP_URL='https://example.com'

# Boolean values
DEBUG=true
MAINTENANCE=false

# Null value
CACHE_DRIVER=null

# Variable expansion
APP_URL=https://example.com
API_URL=${APP_URL}/api

# Multi-line values (with quotes)
DESCRIPTION="This is a description
on multiple lines"

๐Ÿ”’ Security

  • Variables are loaded into $_ENV and $_SERVER
  • Immutable mode by default (does not replace existing system variables)
  • Variable name validation (alphanumeric characters and underscores only)

๐Ÿ”— Integration with Other Packages

Integration with core-php

core-php automatically includes php-dotenv. Use loadEnv() to load variables.

<?php

use JulienLinard\Core\Application;

$app = Application::create(__DIR__);

// Load the .env file
$app->loadEnv();

// Variables are now available in $_ENV
$dbHost = $_ENV['DB_HOST'];
$dbName = $_ENV['DB_NAME'];

Standalone Usage

php-dotenv can be used independently of all other packages.

<?php

require_once __DIR__ . '/vendor/autoload.php';

use JulienLinard\Dotenv\Dotenv;

// Load the .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Access variables
echo $_ENV['DB_HOST'];
echo $_ENV['DB_NAME'];

Usage with Other Frameworks

<?php

// Laravel, Symfony, or any PHP framework
use JulienLinard\Dotenv\Dotenv;

Dotenv::createImmutable(__DIR__)->load();

// Variables are now available
$config = [
    'database' => [
        'host' => $_ENV['DB_HOST'],
        'name' => $_ENV['DB_NAME'],
        'user' => $_ENV['DB_USER'],
        'password' => $_ENV['DB_PASS']
    ]
];

๐Ÿ“š API Reference

Dotenv::createImmutable(string $path, string $file = '.env'): Dotenv

Creates an immutable instance that does not replace existing variables.

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv = Dotenv::createImmutable(__DIR__, '.env.local');

Dotenv::createMutable(string $path, string $file = '.env'): Dotenv

Creates a mutable instance that replaces existing variables.

$dotenv = Dotenv::createMutable(__DIR__);

load(): void

Loads the .env file and sets environment variables in $_ENV and $_SERVER.

$dotenv->load();

required(array $variables): Validator

Validates that the specified variables exist. Throws an exception if a variable is missing.

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

get(string $key, mixed $default = null): mixed

Retrieves a variable with an optional default value.

$dbHost = Dotenv::get('DB_HOST', 'localhost');
$dbPort = Dotenv::get('DB_PORT', 3306);

๐Ÿ’ก Advanced Usage Examples

Validation with Default Values

use JulienLinard\Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Validate with default value
$dotenv->required(['DB_PORT'])->notEmpty()->defaultTo('3306');

Conditional Loading

// Load .env.local if available, otherwise .env
$envFile = file_exists(__DIR__ . '/.env.local') ? '.env.local' : '.env';
$dotenv = Dotenv::createImmutable(__DIR__, $envFile);
$dotenv->load();

Usage in a CLI Script

#!/usr/bin/env php
<?php

require_once __DIR__ . '/vendor/autoload.php';

use JulienLinard\Dotenv\Dotenv;

// Load environment variables
Dotenv::createImmutable(__DIR__)->load();

// Use variables
echo "Database connection: " . $_ENV['DB_HOST'] . "\n";

๐Ÿงช Tests

composer test

๐Ÿ“ License

MIT License - See the LICENSE file for more details.

๐Ÿค Contributing

Contributions are welcome! Feel free to open an issue or a pull request.

๐Ÿ’ Support the project

If this bundle is useful to you, consider becoming a sponsor to support the development and maintenance of this open source project.

Developed with โค๏ธ by Julien Linard