1

I run into this issue after I loaded my custom .env.custom file in AppServiceProvider:

class AppServiceProvider extends ServiceProvider
{
    // ...

    public function boot()
    {
        $host = request()->getHost();

        // read domain based .env file, like: .env.example.com
        $dotenv = \Dotenv\Dotenv::createMutable(base_path(), '.env.' . $host);
        $dotenv->load();
    }
}

(I tried with \Dotenv\Dotenv::createImmutable() too, with same result.)

Then in a controller:

dd(
    env('S3_ENDPOINT'),
    Storage::disk('s3'),
);

The env() return with the new value, but the Storage::disk('s3') contains the old value.

How can I force the whole Laravel system to reload the configuration from the .env.custome file?

netdjw
  • 5,419
  • 21
  • 88
  • 162
  • Did you try `php artisan config:cache` to clear and rebuild the configuration cache? See [Laravel docs](https://laravel.com/docs/8.x/configuration#configuration-caching) for details. – codedge Feb 07 '21 at 14:53
  • try to do `Artisan::call('config:clear');` after loading the env, I think your new value is not loaded in config – bhucho Feb 07 '21 at 16:16
  • I have several .env files, each one contains the specific domain's configuration. After Laravel loaded with .env file it sould be reload the specific env file on the hostname (for example from this file: .env.example.com). The `config:cache` and `config:clear` are command line tools, but I load my specified .env file in runtime. – netdjw Feb 07 '21 at 16:56
  • the cache is saved for all the configuration by laravel to save time, so the s3 value in config/filesystems.php is already stored so you need to clear the cache, as I have said `Artisan::call('config:clear');` you can use artisan class to clear the config, if you don't want that use `Config::set('filesystems.disks.s3', put your env value here);` – bhucho Feb 07 '21 at 17:13
  • I tried the `Artisan::call('config:clear');` before the `Dotenv::createMutable()` code, but nothing changed. – netdjw Feb 07 '21 at 17:16
  • Anyway the `Config::set('filesystems.disks.s3', put your env value here);` is working, thanks! But I don't think this is the normal way how this env file reloading should be working... – netdjw Feb 07 '21 at 18:06
  • why do you not think this can be the normal way ?, Atleast it is better than to clear cache as clearing & reloading cache take few seconds & we only need to change one value – bhucho Feb 07 '21 at 18:46
  • Also you need to clear cache after new env is loaded, @netdjw you wrote `before the` env is loaded – bhucho Feb 07 '21 at 19:11

1 Answers1

0

There are two ways

1. Clear the cache using Artisan class

But it will clear the cache & add again & takes some time.

 $host = request()->getHost();
 $dotenv = \Dotenv\Dotenv::createMutable(base_path(), '.env.' . $host);
 $dotenv->load();
 // clear it after loading the env
 Artisan::call('config:clear'); // if gives error Artisan class not found then use \Artisan::call('config:clear');

2. Set that particular config

Much better as you do not change all config files just the one you want to change. Will takes lesser time.

$host = request()->getHost();
$dotenv = \Dotenv\Dotenv::createMutable(base_path(), '.env.' . $host);
$dotenv->load();
Config::set('filesystems.disks.s3', env("S3_ENDPOINT"));
bhucho
  • 3,903
  • 3
  • 16
  • 34