'Laravel -- I am trying to load another env file that is inside a folder
I've been trying to resolve this issue for two days now. I'd really appreciate some help.
We are going to use a single codebase for multiple domains. I can not use any multitenancy packages available because our codebase is currently a hybird one which is mixed with codeigniter.
So I wrote a package that creates env files under the app root as .envs/.env.{$id}
... so they are all in the .envs
folder. My package creates the env files etc and has everything I need on the created env file.
I wrote a middleware for the same package to make laravel load the created env file. this is where it does not work. My package's service provder
namespace ODS\Tenant;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\ServiceProvider;
use ODS\Tenant\Console\CreateEnvFileCommand;
use ODS\Tenant\Http\Middleware\LoadTenantConfig;
class TenantServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('tenantmanager', function ($app) {
return new TenantManager();
});
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'tenant');
}
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
CreateEnvFileCommand::class,
]);
}
$kernel = app()->make(Kernel::class);
$kernel->pushMiddleware(LoadTenantConfig::class);
}
}
and the middleware for the package
<?php
namespace ODS\Tenant\Http\Middleware;
use Closure;
use Dotenv\Dotenv;
use ODS\Tenant\TenantManager;
use ODS\Tenant\EnvFileManager;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Artisan;
class LoadTenantConfig
{
public function handle($request, Closure $next)
{
$did = TenantManager::findDistrictId();
$envFile = App::basepath()."/.envs/.env.{$did}";
if (! is_file($envFile)) {
$envFileManager = new EnvFileManager($did);
$envFileManager->makeEnvFile($did);
}
$dotenv = Dotenv::createMutable(App::basePath().'/.envs', ".env.{$did}");
$dotenv->load();
Artisan::call('config:clear');
Artisan::call('cache:clear');
App::useEnvironmentPath(App::basePath() . '/.envs');
App::loadEnvironmentFrom(App::basePath(). "/.envs/.env.{$did}");
// App::configPath($envFile);
// dd(App::environmentFile());
// dd(config('app'));
return $next($request);
}
}
What is interesting is that in the middleware above, couple of lines before the end of file, I have dd(App::environmentFile());
and it actually returns the new env file under the .envs folder.
but
in a random controller when I do something like echo config('app.name');
, it is still the value from the default .env
file. I do not understand what is happening. When I do dd($_ENV);
in my controller, I see the values so the Dotenv part is working. I have in my config files to use the env or default to value but it is using the env values from the default .env
file instead of the .envs/.env.{$id}
file
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|