laravel-5phpdotenv

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?


Solution

  • 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')