Env Methods
Core Methods
Section titled “Core Methods”| Method | Description |
|---|---|
$env->get($key, $default) | Get environment variable with type coercion |
$env->has($key) | Check if variable exists |
$env->set($key, $value) | Set environment variable |
Type Coercion
Section titled “Type Coercion”The get() method automatically converts string values:
| String Value | Converted To |
|---|---|
'true' | true (boolean) |
'false' | false (boolean) |
'null' | null |
'empty' | '' (empty string) |
| Other values | Returned as-is (string) |
Access via App
Section titled “Access via App”The app() singleton provides direct access to environment variables:
$debug = app()->env('DEBUG', false);$databaseUrl = app()->env('DATABASE_URL');Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”app()->get('/config', function(Env $env) { return [ 'debug' => $env->get('DEBUG', false), 'region' => $env->get('FLY_REGION', 'unknown'), ];});In Providers
Section titled “In Providers”class DatabaseProvider{ public function __construct(private Env $env) {}
public function __invoke(App $app): void { $app->singleton(DB::class, fn() => new DB( $this->env->get('DATABASE_URL') )); }}In Bindings
Section titled “In Bindings”app()->singleton(DB::class, fn() => new DB( app()->env('DATABASE_URL')));
app()->singleton(Cache::class, fn() => new RedisCache( app()->env('REDIS_URL'), app()->env('CACHE_PREFIX', 'app')));Checking Existence
Section titled “Checking Existence”if ($env->has('STRIPE_KEY')) { $stripe = new StripeClient($env->get('STRIPE_KEY'));}Setting Values (for testing)
Section titled “Setting Values (for testing)”$env->set('APP_ENV', 'testing');$env->set('DEBUG', 'true');Type Coercion Examples
Section titled “Type Coercion Examples”// .env file:// DEBUG=true// WORKERS=5// API_KEY=secret
$debug = $env->get('DEBUG'); // true (boolean, not string)$workers = $env->get('WORKERS'); // '5' (string)$apiKey = $env->get('API_KEY'); // 'secret' (string)