虫言虫语 虫言虫语

Laravel 框架之配置文件以及加载

in 默认分类,PHPread (76110) 文章转载请注明来源!

今天分享一下laravel框架的配置文件的使用以及加载!

首先来看一下laravel文件是如何加载的,之后的使用以及自己定义配置文件就变得很轻松了,好了,下面来看一下吧!

首先到boostrap文件夹下的app.php,先是让容器绑定kernel类吧

$app->singleton(
    IlluminateContractsHttpKernel::class,
    AppHttpKernel::class
)

打开IlluminateFoundationHttpKernel.php,知道下面这段代码

protected $bootstrappers = [
        'IlluminateFoundationBootstrapDetectEnvironment',
        'IlluminateFoundationBootstrapLoadConfiguration',//加载配置文件
        'IlluminateFoundationBootstrapConfigureLogging',
        'IlluminateFoundationBootstrapHandleExceptions',
        'IlluminateFoundationBootstrapRegisterFacades',
        'IlluminateFoundationBootstrapRegisterProviders',
        'IlluminateFoundationBootstrapBootProviders',
    ];

打开IlluminateFoundationBootstrapLoadConfiguration.php文件

public function bootstrap(Application $app)
    {
        $items = [];
        if (file_exists($cached = $app->getCachedConfigPath())) {//如果bootstropcacheconfig.php存在就是加载进去
            $items = require $cached;
            $loadedFromCache = true;
        }
        $app->instance('config', $config = new Repository($items));//将confi绑定到app实例
        if (! isset($loadedFromCache)) {
            $this->loadConfigurationFiles($app, $config);//加载配置文件
        }
        $app->detectEnvironment(function () use ($config) {
            return $config->get('app.env', 'production');
        });
        date_default_timezone_set($config['app.timezone']);
        mb_internal_encoding('UTF-8');
    }

进入到loadConfigurationFiles方法

 foreach ($this->getConfigurationFiles($app) as $key => $path) {
            $repository->set($key, require $path);
        }//这个方法看起来似乎就是一个循环而已

在进入到getConfigurationFiles这个最重要的方法!//这个方法要分解下来看一下!

protected function getConfigurationFiles(Application $app)
    {
        $files = [];
        $configPath = realpath($app->configPath());//获取config文件夹的路径
        foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) {
            $nesting = $this->getConfigurationNesting($file, $configPath);
            $files[$nesting.basename($file->getRealPath(), '.php')] = $file->getRealPath();
        }
        return $files;
    }

首先看一下这个Finder::create()->files()->name('*.php')->in($configPath),进入到Finder看到in方法

public function in($dirs)
    {
        $resolvedDirs = array();
        
        foreach ((array) $dirs as $dir) {
            if (is_dir($dir)) {
                $resolvedDirs[] = $dir;
            } elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
                $resolvedDirs = array_merge($resolvedDirs, $glob);
            } else {
                throw new InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
            }
        }
        $this->dirs = array_merge($this->dirs, $resolvedDirs);
        return $this;
    }//返回时Finder实例!具体请打印看一下,一目了然

既然返回的是对象实例,为何能直接foreach呢?没错他继承了内置的一个接口IteratorAggregate,继承这个接口的话必须要实现getIterator这个方法,具体关于这个接口请参考php官方手册!

代码我就不贴了,挺多的,看一下官方返回的说明吧* @return Iterator|SplFileInfo[] An iterator!返回也是对象实例!

打印一下就可以了,再回头看getConfigurationFiles方法,就一目了然了!这个方法就是将config文件夹的.php文件结尾的文件组装到数组中返回!

再通过loadConfigurationFiles这个方法加载到应用当中!再通过$repository->set($key, require $path);将config文件和文件名以键值对的方法注入到items这个属性数组里面,具体可以追踪到IlluminateConfigRepository.php文件里面看一下!

这样说下来,就很简单了吧!你什么都不需要做,只需要你在config里面添加配置文件就可以了!随便添加,框架都会检测到,帮你加载进来!

比如你添加一个xxx.php的config文件,里面是:

<?
    return [
    'a' => 'b'];

你可以使用助手函数config这样获取config('xxx.a');输入一下就会获取到b!就是这么简单!超级方便有没有!简介明了

jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

发表新评论
已有 10613 条评论
  1. JbdcSkype
    JbdcSkype
    回复

    viagra superforce online viagra overnight delivery in us viagra in the uk http://llviabest.com/ - viagra buy payoal ’

  2. TadSa
    TadSa
    回复

    cialis pasalinis poveikis buy cialis tadalafil tablets meglio viagra cialis levitra

  3. 1stmodafinilBoima

    modafinil for studying order modafinil does modafinil cause weightloss
    modafinil when sick

  4. TadSa
    TadSa
    回复

    cialis doctors buy cialis drug cialis nadelen

  5. Hynugl
    Hynugl
    回复

    ed drugs - http://erectileprop.com/ new ed pills

  6. 1stmodafinilBoima

    too much modafinil cheap modafinil provigil and depression treatment
    where to get modafinil us 2018 reddit

  7. TadSa
    TadSa
    回复

    le cialis 5mg canadian pharmacy cialis 20mg interazione coumadin cialis

  8. GetSa
    GetSa
    回复

    cialis rebate coupon generic cialis canadian cialis causa priapismo

  9. 1stmodafinilBoima

    canadian pharmacy modafinil modafinil 200mg how to convert adrafinil to modafinil
    where to buy buy modafinil online

  10. GetSa
    GetSa
    回复

    cialis falsifikat cialis soft tablet cialis brand names in india

  11. GetSa
    GetSa
    回复

    buy cialis in vancouver generic cialis daily pricing bintang mawar cialis

  12. TadSa
    TadSa
    回复

    cialis powder recipe taking cialis soft tabs cialis causa hemorroidas

  13. FsbxSkype
    FsbxSkype
    回复

    pfizer viagra viagra over the counter walgreens viagra ireland online http://genericrxxx.com/ - melbourne viagra ’

博客已萌萌哒运行
© 2021 由 Typecho 强力驱动.Theme by Yodu
前篇 后篇
雷姆
拉姆