Wednesday, August 20, 2025

Variable Expansion in Laravel .env (a few powerful patterns)

Laravel’s .env files can do more than just hold static values. With variable expansion, you can reference one environment variable inside another—making your configuration cleaner, more consistent, and easier to manage across environments.

Variable Expansion in Laravel .env (a few powerful patterns)

Environment variables are “just strings,” but you can reference one env var inside another to avoid duplication and keep things tidy. Laravel uses phpdotenv under the hood, which supports variable expansion with the ${VAR_NAME} syntax.

Rule of thumb: reference already-defined variables. Expansion happens as the file is parsed, top-to-bottom. Circular references won’t resolve.

Reuse your app name in other values

APP_NAME=Ghostable
APP_LOG_FILE=${APP_NAME}_app.log
// config/logging.php
'channels' => [
    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/' . env('APP_LOG_FILE', 'laravel.log')),
    ],
],

Why it’s nice: rename the app once; log files and other labels follow automatically.

Build service URLs from one base

APP_DOMAIN=ghostable.dev
APP_URL=https://${APP_DOMAIN}

API_SUBDOMAIN=api
API_URL=https://${API_SUBDOMAIN}.${APP_DOMAIN}

DOCS_SUBDOMAIN=docs
DOCS_URL=https://${DOCS_SUBDOMAIN}.${APP_DOMAIN}
// config/app.php (or a custom config/services.php)
'url'  => env('APP_URL'),
'api'  => ['url' => env('API_URL')],
'docs' => ['url' => env('DOCS_URL')],

Why it’s nice: change APP_DOMAIN for staging/preview environments and everything else updates without hunting through 15 keys.

One flag, many features (prefix pattern)

FEATURE_PREFIX=ghostable
REDIS_CACHE_PREFIX=${FEATURE_PREFIX}_cache_
REDIS_QUEUE_PREFIX=${FEATURE_PREFIX}_queue_
SESSION_PREFIX=${FEATURE_PREFIX}_session_
// config/cache.php
'prefix' => env('REDIS_CACHE_PREFIX', 'laravel_cache_'),

// config/queue.php
'connections' => [
    'redis' => [
        'connection' => 'default',
        'retry_after' => 90,
        'block_for' => null,
        'prefix' => env('REDIS_QUEUE_PREFIX', 'laravel_queue_'),
    ],
],

// config/session.php
'cookie' => env('SESSION_PREFIX', 'laravel_session') . 'id',

Why it’s nice: change one prefix to isolate environments (PR previews, tenants, branches) without editing three configs.

Gotchas

  • Define before use. ${VAR} only expands if VAR was defined earlier in the file. Keep your “base” vars at the top.
  • Use ${VAR} not $VAR. The curly-brace form is the reliable, supported syntax.
  • Don’t rely on expansion for secrets loading order. If the server provides an env var, it will override the .env value—expansion won’t change that precedence.
  • Config cache still matters. Expansion resolves when the .env is parsed, but you should still pull values from config() in your app (not env()), especially in production with config:cache.

Want product news and updates?

Sign up for our newsletter.

Email Address

We care about your data. Read our privacy policy.