10

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
apokryfos
  • 38,771
  • 9
  • 70
  • 114

1 Answers1

14

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')
Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
esemve
  • 360
  • 2
  • 10
  • 2
    If you want the environment variables to be available for use in your app.php config file, then add these two lines at the very beginning of app.php instead of in AppServiceProvider (since the app.php config file is executed before the booth method in AppServiceProvider) – Daniel L Nov 30 '17 at 17:20
  • 2
    This answer is correct for laravel versions 5 - 5.7 but since version 5.8 Laravel has been using phpdotenv 3 which needs to be created as `\DotEnv\DotEnv::create(base_path(), '.env.rdate');` instead of using `new`. – apokryfos Oct 01 '19 at 13:06