Revision: Thu, 25 Apr 2024 11:06:46 GMT
v2.14 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.12
Edit this page

Extensions - Dotenv

Default application skeleton includes the ability to read ENV values from .env file located in the root of the project.

Configuration

To install Dotenv extension in non-default application skeleton:

composer require spiral/dotenv-bridge

Add the following bootloader to your application:

php
    protected const LOAD = [
        Spiral\DotEnv\Bootloader\DotenvBootloader::class,
        
        //...
    ]

Note
Make sure to add a bootloader at the top of the list to alter Env.

The values from the .env the file will be copied into your env and available via Spiral\Boot\EnvironmentInterface or env function.

Note
Attention, this component overwrites system ENV values.

Pre-Processing

Please remember that values in .env will be pre-processed, following changes will occur:

Value PHP Value
true true,
(true) true,
false false,
(false) false,
null null,
(null) null,
empty ''

Note
The quotes around strings will be stripped automatically.

Disabling overwriting ENV variables

By default, the new values overwrite the previously set ones. This behavior can be changed by setting the overwrite parameter to false in the Spiral\Boot\Environment class:

php
use Spiral\Boot\Environment;

// in the run method
$app = App::create([...]);
$app->starting(...);
$app->started(...);
$app->run(new Environment([
     'APP_ENV' => 'production'
], overwrite: false));

// or in the init method
$app = App::init(
    ['root' => __DIR__], 
    new Environment(overwrite: false)
);