虫言虫语 虫言虫语

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

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

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

支付宝打赏

文章二维码

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

发表新评论
已有 24872 条评论
  1. HenryKam
    HenryKam
    回复

    is amoxicillin safe during pregnancy
    acne amoxicillin

  2. Myrongrorn
    Myrongrorn
    回复

    personals sites
    local personals

  3. HenryKam
    HenryKam
    回复

    natural substitute for amoxicillin
    maculopapular rash amoxicillin

  4. HenryKam
    HenryKam
    回复

    bronchitis amoxicillin dosage
    amoxicillin yeast infection

  5. zyloprim
    zyloprim
    回复

    tablets http://cialis20walmart.com - zyloprim zlote zyloprim token

  6. smagmevagype
    smagmevagype
    回复

    viagra price https://viagrasildenafilok.com/ - viagra prices viagra cheap viagra

  7. GeorgeErugh
    GeorgeErugh
    回复

    https://internationalwealth.info/foreign-bank-accounts/how-to-open-a-foreign-bank-account-to-a-physical-person/
    https://internationalwealth.info/foreign-bank-accounts/how-to-open-an-account-in-a-swiss-bank-for-individuals-in-2020/
    https://internationalwealth.info/offshore-for-newbies/what-is-offshore-in-simple-terms/
    https://internationalwealth.info/offshore-for-newbies/how-to-move-money-offshore-from-offshore/
    https://internationalwealth.info/foreign-bank-accounts/kak-otkryt-schet-v-shvejcarskom-banke-chastnomu-licu-iz-rossii/
    https://internationalwealth.info/foreign-bank-accounts/mozhno-li-otkryt-depozit-v-inostrannom-banke/
    https://internationalwealth.info/offshore-company-formation/15-reasons-to-register-company-in-cyprus/
    https://internationalwealth.info/offshore-company-formation/how-to-move-your-business-offshore-and-wheather-you-should-do-it/
    https://internationalwealth.info/foreign-bank-accounts/kak-otkryt-schet-v-kiprskom-banke-v-2020-godu/
    https://internationalwealth.info/foreign-bank-accounts/opening-a-bank-account-in-the-uae-in-2020/
    https://internationalwealth.info/anonymous-debit-card/anonymous-debit-card/
    https://internationalwealth.info/offshore-banks/how-to-open-account-in-offshore-bank-for-physical-persons-in-2019/

  8. swimarumTauff

    https://viagrasildenafilok.com/ viagra prescription viagra price buy sildenafil viagra online

  9. canadian pharmacies online

    mexican online pharmacies
    http://canadianbestpharmacyonline.com/
    canada drug

  10. CNoloWotmeDo
    CNoloWotmeDo
    回复

    community america fax number positive words pictures , ivermectin for sale ivermectin and covid nc community college biology jobs. planning process hrm community aid thrift store lancaster . followers church, community action keokuk iowa community action agency brewton al individual player record national level social repose US 88deb6_ , culture of safety, community acquired pneumonia contagious .

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