Laravel 支持动态多 env 配置读取 
                                                    
                        
                    
                    
  
                    
                    需要注意的是写这篇笔记的时候对应
Laravel版本是6.x
如何支持多 ENV 文件?
首先通过搜索引擎了解到可以通过在启动文件 bootstrap/app.php 中调用如下方法重新指定要读取的配置文件:
$app->loadEnvironmentFrom(
     '自定义.env'
);但是随之而来的就是报错因为网上的文档是基于 5.x 的版本,于是就利用 PHPStorm 的追踪功能进入了其源码中:
/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php /**
 * Set the directory for the environment file.
 *
 * @param  string  $path
 * @return $this
 */
public function useEnvironmentPath($path)
{
    $this->environmentPath = $path;
    return $this;
}
/**
 * Set the environment file to be loaded during bootstrapping.
 *
 * @param  string  $file
 * @return $this
 */
public function loadEnvironmentFrom($file)
{
    $this->environmentFile = $file;
    return $this;
}注意到此方法的上一行方法了么?没错看注释就已经非常明白了:设定读取配置文件的目录,所以只要将这两个搭配起来使用即可实现读取自定义的 env 配置文件:
$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// 此处为新增的改写部分:基于域名来读取 __env 目录下的配置文件
$app->useEnvironmentPath(
    __DIR__ . '/../__env'
);
$app->loadEnvironmentFrom(
     '.' . $_SERVER['SERVER_NAME'] .'.env'
);
参考文献
本作品采用《CC 协议》,转载必须注明作者和本文链接
 
           阿麦 的个人博客
 阿麦 的个人博客
         
             
                     
                     
             
             
           
           关于 LearnKu
                关于 LearnKu
               
                     
                     
                     粤公网安备 44030502004330号
 粤公网安备 44030502004330号 
 
推荐文章: