虫言虫语 虫言虫语

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

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

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

支付宝打赏

文章二维码

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

发表新评论
已有 477 条评论
  1. RonaldosThoni

    what is price of cialis
    http://cialisle.com/# - 5mg cialis
    herbal equivalent of cialis
    tadalafil citrate
    cialis antibiotics

  2. Williemaeyqsr

    Buy Silagra order (Sildenafil Citrate) low cost worth on-line for sale cheap Silagra is prescribed for erectile dysfunction remedy. Silagra Tablets 100mg are used to treat erectile dysfunction (male impotence). Silagra® (Sildenafil Citrate 100mg tablets) … An essential notice for that men probably to buy Silagra may be the following - it’s much less expensive than Viagra. Buy Silagra one hundred mg with sildenafil citrate online form PremiumRxDrugs.If you loved this post and you would like to acquire additional data with regards to online viagra with doctor https://gncedstore.com kindly check out the web-page.

  3. Stevnisa
    Stevnisa
    回复

    Cealis 30 Tablet Trial http://cheapcialisir.com - Cheap Cialis Acquistare Consegna Kamagra 24h Cialis Comprar Viagra Generico Seguro

  4. Lesswesty
    Lesswesty
    回复

    Viagra Yahoo Respuestas http://abuycialisb.com - Cialis Cialis Generique En 24h Buy Cialis Propecia 0.4 Mg

  5. Stevnisa
    Stevnisa
    回复

    Discount Celebrex Canada http://viacialisns.com/# - cialis 5mg best price Free Samples Of Cialis Cialis Comparatif Prix Viagra

  6. TerryasFep
    TerryasFep
    回复

    cialis with klonopin
    https://www.liverichandfree.com/# - online tadalafil
    cialis upsets my stomach
    cialis usa
    how to get your insurance to pay for cialis

  7. Lesswesty
    Lesswesty
    回复

    Cheap Viagra Without Prescription http://cheapcialisir.com - buy cialis online Cialis Generico In Farmacia Cheap Cialis Brand Name Propecia

  8. Lesswesty
    Lesswesty
    回复

    Mexico Pharmacy https://buyciallisonline.com/# - Cialis Tegretol Cialis Propecia Vergessen Einzunehmen

  9. Stevnisa
    Stevnisa
    回复

    Productividad Propecia https://buyciallisonline.com/# - Buy Cialis Is Outdated Amoxicillin Safe Cialis Viagra Arzneimittelgesetz

  10. Lesswesty
    Lesswesty
    回复

    Viagra 100 Mg Forum http://buycialisuss.com - Buy Cialis Generique Du Cialis 5mg Buy Cialis Comprar Cialis 20 Mg Original

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