7

I need to load a different .env file, named .env.test under certain conditions.

I attempted to do that through a middleware by adding

app()->loadEnvironmentFrom('.env.test');
Dotenv::create(base_path(), '.env.test')->overload();

to the bootstrap() method of Kernel.php. I also tried to create a dedicated middleware for this and load it as the first one in the web middleware group. But either way, it is loading the standard .env file.

It works if I do it in the /bootstrap/app.php file but I really don't want to put it there.

TheNiceGuy
  • 3,462
  • 8
  • 34
  • 64
  • Do you want to load .env.test file instead of .env or both files? – HiKangg Oct 02 '20 at 17:01
  • Have you made sure you aren't caching your configuration? https://laravel.com/docs/8.x/configuration#configuration-caching – miken32 Oct 02 '20 at 17:01
  • It will always load the `.env` file. You can load additional files using the code you've shared. I don't think you can choose a different .env file to load at the middleware stage though, the .env file has already been loaded at that point – apokryfos Oct 02 '20 at 17:04
  • @HiKangg: I want to load it instead of the default one – TheNiceGuy Oct 02 '20 at 17:07
  • @miken32: No, it is not cached – TheNiceGuy Oct 02 '20 at 17:07
  • @apokryfos From what I understand, the dotenv overload() method should take care of that? – TheNiceGuy Oct 02 '20 at 17:08
  • Overload *should* overwrite environment variables from your `.env.test` file that have already been loaded via the `.env` file (in addition to loading new variables that were not loaded before). Is that not working? – apokryfos Oct 02 '20 at 17:09
  • @apokryfos: It depends how you look at it. It works if I access the environment variable inside of a controller. What I want to accomplish though, is to make sure that the .env.test file is used for the sessions, meaning that it should use the APP_KEY from the .env.test file instead of the default .env file – TheNiceGuy Oct 02 '20 at 17:13
  • If I had to guess, I'd say the problem is that the application config is already generated by the time the middleware is run, as @apokryfos says. Nothing is pulling directly from the `.env` file except for things in the `config` directory. – miken32 Oct 02 '20 at 17:24
  • I just found the solution, see the answer. Thanks! – TheNiceGuy Oct 02 '20 at 17:35

3 Answers3

5

I just figured it out: The default .env file is being loaded inside of the bootstrap() method of LoadEnvironmentVariables.php.

To use the .env.test file I had to restructure my initial bootstrap() method inside of the App/Http/Kernel.php file to look like this:

public function bootstrap() 
{
    app()->loadEnvironmentFrom('.env.test');
    parent::bootstrap(); 
}

So the essential part was to move the parent::bootstrap() call below the loadEnvironmentFrom() call.

TheNiceGuy
  • 3,462
  • 8
  • 34
  • 64
4

Instead of doing any code change, you can use export command create a file called .env.test, you want to sue this as .env file use terminal

  1. APP_ENV=local

  2. php artisan config:cache

  3. php artisan key:generate

This below edit is to explain how .env file is getting set In Illuminate\Foundation\Application class has method loadEnvironmentFrom which is taking the file as parameter and setting it,

you can use bootstrap/app.php after you are getting $app

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
 );

here you will be having instance of Illuminate\Foundation\Application

you can just call the loadEnvironmentFrom function like

$app->loadEnvironmentFrom('.env.local');

May be it is better to use Kernel.php instead of this, I do not think either of bootstrap/app.php or kernel.php will get overridden with composer update, so make more calculation while using it. I have added this, so that it will help you understand the stuffs.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
2

You can load a different environment file using APP_ENV

For example if APP_ENV=test then .env.test can be loaded.

More info: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php#L41

David Thomas
  • 4,027
  • 3
  • 28
  • 22