Never Use env() Outside Config Files + Enforce with Pest Arch

Punyapal Shah
env() returns null when config is cached in production. Always use config() and enforce this with Pest architecture tests.

In production environments running php artisan config:cache, calling env() outside files in config/*.php returns null. Enforce this rule across your codebase using Pest architecture testing.

// tests/ArchitectureTest.php
arch('avoid env outside config')
    ->expect('env')
    ->not->toBeUsed()
    ->ignoring('config');
  • env() returns null in production when config:cache is active
  • Always define configuration keys in config/*.php files and call config('key')
  • Pest arch tests catch leftover env() calls before deployment
Tags: #Laravel #Architecture #Pest
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips