Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpdotenv Laravel Package

vlucas/phpdotenv

vlucas/phpdotenv loads environment variables from a .env file into getenv(), $_ENV, and $_SERVER. Ideal for keeping secrets out of code and managing per-environment configuration in PHP apps via simple, portable dotenv files.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require vlucas/phpdotenv

Then create a .env file in your project root (outside version control) with key-value pairs:

APP_ENV=local
DB_HOST=localhost
DB_PASSWORD=supersecret

Also create a .env.example file (checked into VCS) with placeholders to guide collaborators.
Load env vars early in your app’s lifecycle:

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

Access variables via $_ENV['KEY'], $_SERVER['KEY'], or (with createUnsafeImmutable) getenv('KEY').

Implementation Patterns

  • Laravel Integration: Laravel ships with DotEnv preconfigured in bootstrap/app.php. Use config() with env() behind the scenes — but avoid calling env() outside config files (it may be cached/undefined in production).
  • Validation on Load: Enforce required/typed vars immediately after load():
    $dotenv->required(['APP_KEY', 'DB_HOST'])->notEmpty()->isString();
    $dotenv->required('APP_DEBUG')->isBoolean();
    
  • Per-Environment Configs: Switch between .env.local, .env.staging, etc. by passing filename:
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__, $_ENV['APP_ENV_FILE'] ?? '.env');
    $dotenv->load();
    
  • Repository Customization: For advanced control (e.g., strict immutability, allow-listing):
    $repo = Dotenv\Repository\RepositoryBuilder::createWithDefaultAdapters()
        ->immutable()
        ->allowList(['APP_', 'DB_'])
        ->make();
    $dotenv = Dotenv\Dotenv::create($repo, __DIR__)->load();
    
  • Testing: Use .env.testing or override in tests by calling createMutable() before loading.

Gotchas and Tips

  • Immutability Matters: createImmutable() (default) refuses to overwrite existing env vars (e.g., set via Apache/Nginx). Use createMutable() only if you must override system vars — but be cautious for security.
  • getenv() vs Superglobals: Superglobals ($_ENV, $_SERVER) are populated during load; getenv() reflects current state. For CLI scripts, prefer superglobals (they’re populated reliably).
  • PHP Compatibility: Ensure variables_order in php.ini includes E and S (e.g., variables_order = "EGPCS"); otherwise $_ENV/$_SERVER may be empty.
  • Multi-line Values: Only quote values containing spaces or special chars — avoid unquoted values starting with # or =. Nested vars (${FOO}) are resolved at parse time.
  • Production Caution: .env is for development/local setups. In production, prefer native env vars (set by process manager, container, or cloud platform) — don’t deploy .env files.
  • Validation Pitfalls: isInteger() expects an integer string (e.g., "123"), not floats like "12.0". Use isNumeric() for floats/ints.
  • Security: .env should be in .gitignore. Never commit secrets. Use .env.example + documentation to guide onboarding.
  • Debugging: To inspect parsed vars without loading:
    $vars = Dotenv\Dotenv::parse(file_get_contents(__DIR__.'/.env'));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope