PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 14, 2022

[FIXED] Laravel - Load different .env file

 February 14, 2022     laravel, laravel-6, php     No comments   

Issue

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.


Solution

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.



Answered By - TheNiceGuy
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing