虫言虫语 虫言虫语

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

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

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

支付宝打赏

文章二维码

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

发表新评论
已有 5035 条评论
  1. NickDes
    NickDes
    回复

    cost of zoloft 20 mg medication gabapentin 300 mg capsule inderal 160 mg capsule buy chloroquine canada ceftin lowest price bactrim ds price dapoxetine tablets for sale bactrim 500 mg tablet

  2. dhgnkvlrrsaa
    dhgnkvlrrsaa
    回复

    buy generic cialis canadian pharmacy viagra online pharmacy canada pharmacy for cialis canadian pharcharmy online viagra canadian pharcharmy kuuvyd
    cialis and pharmacy

  3. Getting A Loan
    Getting A Loan
    回复

    best life insurance policy

  4. qxkwrpwwncjp
    qxkwrpwwncjp
    回复

    canadian pharmacy reviews canadian pharmacy cialis for daily use brand viagra online canada pharmacy northwestpharmacy.com scam
    canadian pharmacies shipping to usa london pharmacy canada cvs pharmacy buy cialis from us pharmacy
    canadianpharmacymeds.com canada drug pharmacy canadian pharmacy for viagra canadian online pharmacy viagra viagra pharmacy

  5. Buy Essay Cheap
    Buy Essay Cheap
    回复

    cheap dissertation methods in a research paper

  6. dnmoxgzjfsoh
    dnmoxgzjfsoh
    回复

    canada pharmacies online prescriptions cialis pharmacy rx one canadian pharmacies viagra best canadian online pharmacy reviews viagra online canada pharmacy fijong
    northwest canadian pharcharmy online

  7. https://abcviag.com/

    legitimate online pharmacy viagra
    viagra jamaica
    cipro company registration online roani Cloup

  8. MiaDes
    MiaDes
    回复

    orlistat online canada hydroxychloroquine antiviral fluoxetine 20 mg tablet brand name robaxin 400 mg medrol from canadian pharmacy

  9. KimDes
    KimDes
    回复

    where to buy plaquenil

  10. https://abcviag.com/

    best online viagra canada
    buy viagra in india
    buy viagra us pharmacy roani Cloup

  11. aeoumkliwban
    aeoumkliwban
    回复

    canada pharmacies us pharmacy for cialis northwestern pharmacy canada us pharmacy for cialis
    cialis-canadian pharmacy generic viagra online pharmacy canadian pharmacies viagra canadian pharmacy.com
    canada rx pharmacy pharmaceuticals from canada canadapharmacyonline.com canadian pharmacy cialis online canadian pharcharmy

  12. lohxxtozulcl
    lohxxtozulcl
    回复

    list of canada online pharmacies no prescription pharmacy buy cialis online canada pharmacy canadian viagra online pharmacy reviews canadian pharmacies for viagra ndlfqx
    viagra online pharmacy

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