Declare the variables your PHP or Laravel app needs — types, allowed values, per-environment rules — using the validation rules you already know. Catch mistakes at boot or in CI, not three screens into a user flow.
composer require jfrintrop/env-validator
Real output from the package — pick a scenario.
EnvValidator::make([
'APP_ENV' => 'required|in:local,staging,production',
'APP_DEBUG' => 'required|boolean',
'DB_PORT' => 'required|integer|between:1,65535',
])->validate();
Environment validation failed with 2 problems:
- The APP_ENV environment variable must be one of: local, staging, production.
- The DB_PORT environment variable must be an integer.
EnvValidator::make([
'APP_ENV' => 'required',
'APP_DEBUG' => 'required|boolean',
'STRIPE_KEY' => 'nullable|string',
])->whenEnvironment('production', [
'APP_DEBUG' => 'required|declined',
'STRIPE_KEY' => 'required|starts_with:sk_live_',
])->validate();
Environment validation failed with 2 problems:
- The APP_DEBUG environment variable must be disabled (false, 0, no, off).
- The STRIPE_KEY environment variable is required but missing.
Environment is valid.
php artisan env:validate --strict --env-file=.env.production
Environment validation failed with 1 problem:
- The DB_PROT environment variable is not declared in the schema.
php artisan env:example
# required · one of: local, staging, production APP_ENV=local # required · boolean (true/false) APP_DEBUG=false # required · integer · between 1 and 65535 DB_PORT=
php artisan env:example --check
.env.example is out of sync with the schema:
⇂ DB_PORT is declared in the schema but missing from the file.
⇂ OLD_VAR is in the file but not declared in the schema.
php artisan env:validate --format=json
{
"valid": false,
"checked": 2,
"errors": {
"APP_ENV": [
"The APP_ENV environment variable must be one of: local, staging, production."
],
"DB_PORT": [
"The DB_PORT environment variable must be an integer."
]
}
}
php artisan env:validate --strict --env-file=.env.production --format=github
::error file=.env.production,title=Environment validation::The APP_ENV environment variable must be one of: local, staging, production.
::error file=.env.production,title=Environment validation::The DB_PORT environment variable must be an integer.
- run: php artisan env:validate --strict --env-file=.env.production --format=github - run: php artisan env:example --check --format=github
Laravel validation syntax under the hood — no new DSL.
Handles true/false/null/empty and quoted values like env() does.
Stricter requirements for production, relaxed for local.
Proper exit codes for env:validate and env:example --check.
Opt in to int, float and bool results.
The core needs no framework — any PHP 8.1+ project.