虫言虫语 虫言虫语

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

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

今天分享一下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

支付宝打赏

文章二维码

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

发表新评论
已有 2051 条评论
  1. Brianrenly
    Brianrenly
    回复

    check out the post right here click this over here now what do you think

  2. tadalafil reviews

    Involving at distinctive me. real money casino online casino slot games

  3. Brianrenly
    Brianrenly
    回复

    address more info here page

  4. Brianrenly
    Brianrenly
    回复

    watch this video check that at Bing

  5. vttqabaxy
    vttqabaxy
    回复

    generic viagra walmart http://buyviagricxr.com/ - viagra side effects side effects of viagra generic viagra online viagra generic

  6. vthDrurb
    vthDrurb
    回复

    viagra http://buyviagenonline.com/ - generic viagra walmart buy viagra over the counter viagra cheap viagra

  7. vthDrurb
    vthDrurb
    回复

    herbal viagra http://buyviagenonline.com/ - cheap viagra viagra alternative generic viagra online for sale online viagra

  8. vpkcrure
    vpkcrure
    回复

    how long does viagra last http://cheapgogenvia.com/ - viagra generic name cheap viagra what is viagra viagra vs cialis

  9. vfgabaxy
    vfgabaxy
    回复

    viagra http://cheapvirgaraonline.com/ - generic viagra generic viagra walmart how to get viagra generic viagra online for sale

  10. vfgrPydaY
    vfgrPydaY
    回复

    viagra online http://viagracefo.com/ - viagra what is viagra generic viagra cheap viagra

  11. cialis generic cialis tadalafil

    For pertussis, on the fixed pulmonary that you have a greatly dictatorial using. chumba casino real casino online

  12. vthDrurb
    vthDrurb
    回复

    viagra pill http://buyviagenonline.com/ - otc viagra what is viagra over the counter viagra herbal viagra

  13. grdfscant
    grdfscant
    回复

    how to get viagra http://viagraid.com/ - buy generic 100mg viagra online viagra generic buy generic 100mg viagra online viagra canadian pharmacy ezzz

  14. Brianrenly
    Brianrenly
    回复

    check out the post right here more help navigate to this site

  15. vtgdretelf
    vtgdretelf
    回复

    cialis buy http://cialonlinesja.com/ - generic cialis online cialis cialis online cialis

  16. vpkcrure
    vpkcrure
    回复

    viagra generic http://cheapgogenvia.com/ - generic viagra female viagra how long does viagra last viagra

  17. brliFaulP
    brliFaulP
    回复

    buy cialis http://cialiisensf.com/ - online cialis online cialis online cialis cialis 20mg

  18. erfiUnada
    erfiUnada
    回复

    viagra online http://viagrajr.com/ - cheap viagra generic viagra online viagra generic viagra alternative

  19. cialis generic name

    Sanitarium sildenafil showing and about the tubules micro obstructive. casino online gambling

  20. vttqabaxy
    vttqabaxy
    回复

    buy viagra http://buyviagricxr.com/ - buy viagra viagra side effects of viagra viagra alternative

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