Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dave101ua's avatar

check .env variables

Hello I have lumen application in bootstrap/app.php I have code which loads env variables from .env file via this code

$bootstrap = new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
);
$bootstrap->bootstrap();`

what is good way to check that .env file contains all required variables ?

0 likes
4 replies
tykus's avatar

Do you mean programmatically??? What determines a required environment variable?

dave101ua's avatar

@tykus programmatically required means that this is necessary in order to run application for example I provide a list of mandatory vars and application fails if one of them is not there

dave101ua's avatar

Solution I found


try {
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->required(
        [
            'APP_ENV',
            'DB_HOST',
            'DB_PORT',
            'DB_NAME',
            'DB_USER',
            'DB_PASS'
        ]
    );

} catch (Exception $e) {
    die("Check bootstrap configuration: {$e->getMessage()}");
}

If var from list does not exist then it throws an excetion and you can handle it

Please or to participate in this conversation.