Laravel-config目录中配置文件的加载顺序修改

2017-11-10  本文已影响0人  未来纪元

问题描述:若在config目录下添加config_dev.php , config_test.php , config_pro.php 那么在config目录下使用config()调用前面的三个文件的内容时由于加载顺序的问题可能会导致先加载的问价无法读取后加载的文件的内容

解决方案:在Illuminate\Foundation\Bootstrap\LoadConfiguration.php文件中的getConfigurationFiles()方法中修改配置文件的加载顺序

代码如下:

protected functiongetConfigurationFiles(Application$app)

{

$files= [];

$configPath= realpath($app->configPath());

foreach(Finder::create()->files()->name('*.php')->in($configPath)as$file) {

$nesting=$this->getConfigurationNesting($file,$configPath);

//修改文件加载的顺序,原来的顺序有点问题

$basename= basename($file->getRealPath(),'.php');

if(in_array($basename,['config_dev','config_test','config_pro'])){

$files[0][$nesting.basename($file->getRealPath(),'.php')] =$file->getRealPath();

}else{

$files[1][$nesting.basename($file->getRealPath(),'.php')] =$file->getRealPath();

}

//原代码sss

//            $files[$nesting.basename($file->getRealPath(), '.php')] = $file->getRealPath()s;

}

$files= array_merge($files[0],$files[1]);

return$files;

}

上一篇下一篇

猜你喜欢

热点阅读