Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple env files

I'm wondering if there's a way in Laravel to specify a set of env files to load. My exact problem is I want to add something like a suffix to all my .js and .css resources. Ideally I'd have a suffix like the release date because it would be ok for these files to be cached within the same release but I would like the caches to be invalidated on the next release. However I want to avoid reading, modifying and saving the .env file if possible and would instead prefer to create a new file e.g. .env.rdate which would be generated via a script, e.g.

echo APP_RELEASE_DATE=`date +%s` > env.rdate

or something like this. Is this at all possible or do I have to read/update/write the .env file instead?

like image 794
apokryfos Avatar asked Jan 31 '17 13:01

apokryfos


People also ask

Can I have multiple .env files?

One solution is to have multiple . env files which each represent different environments. In practice this means you create a file for each of your environments: .

Should you use .env in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.

Where should .env files go?

You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.


1 Answers

Create your .env.rdate file next to .env file.

Put this to your AppServiceProvider boot method:

    $dotenv = new \Dotenv\Dotenv(base_path(),'.env.rdate');
    $dotenv->overload(); 

After you can use in your project:

ENV('APP_RELEASE_DATE')
like image 195
esemve Avatar answered Oct 28 '22 16:10

esemve