This is the first day of my participation in the More text Challenge. For details, see more text Challenge

The introduction

  • Config is a common way to get a configuration in Laravel
  • If we can understand how it works, how can we work better
  • It also helps you understand the Laravel framework

1. Loading process

2. When will it be loaded

index.php

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
Copy the code

The kernel is loaded at startup

Http \ \ Illuminate \ Foundation \ Kernel class

protected $bootstrappers = [
        \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, / / load env
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, // Load the configuration file
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];
    
    
protected function sendRequestThroughRouter($request)
    {
        $this->app->instance('request'.$request);

        Facade::clearResolvedInstance('request');

        // This is the main thing
        $this->bootstrap(); . }public function bootstrap()
    {
        if (! $this->app->hasBeenBootstrapped()) {
            $this->app->bootstrapWith($this->bootstrappers()); }}Copy the code

App core classes

Illuminate, Foundation, Application class

public function bootstrapWith(array $bootstrappers) { $this->hasBeenBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this['events']->dispatch('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->dispatch('bootstrapped: '.$bootstrapper, [$this]); }}Copy the code

3. LoadConfiguration source analysis

We finally get to the core load configuration class

, Illuminate, Foundation, the Bootstrap \ LoadEnvironmentVariables class

public function bootstrap(Application $app) { $items = []; If (is_file($cached = $app->getCachedConfigPath())) {$items = require $cached; $loadedFromCache = true; $app->instance('config') $app->instance('config') $app->instance('config') $app->instance('config') $app->instance('config') $app->instance('config') $config = new Repository($items)); // Load if (! isset($loadedFromCache)) { $this->loadConfigurationFiles($app, $config); $app->detectEnvironment(function () use ($config) {return $config->get('app.env', 'production'); }); }Copy the code

LoadConfigurationFiles method

protected function loadConfigurationFiles(Application $app, $files = $this-> getfile ($app); $this-> getfile ($app); if (! isset($files['app'])) { throw new Exception('Unable to load the "app" configuration file.'); } foreach ($files as $key => $path) {$repository->set($key, require $path); }}Copy the code

getConfigurationFiles

protected function getConfigurationFiles(Application $app)
    {
        $files = [];

        $configPath = realpath($app->configPath());

        foreach (Finder::create()->files()->name('*.php')->in($configPath) as $file) {
            $directory = $this->getNestedDirectory($file, $configPath);

            $files[$directory.basename($file->getRealPath(), '.php')] = $file->getRealPath();
        }

        ksort($files, SORT_NATURAL);

        return $files;
    }
Copy the code

4. Config command analysis

  • PHP artisan config:cache

Illuminate\Foundation\Console\ConfigCacheCommand

Public function handle() {$this->call('config:clear'); $config = $this->getFreshConfiguration(); $configPath = $this->laravel->getCachedConfigPath(); $this->files->put($configPath, '<? php return '.var_export($config, true).'; '.PHP_EOL ); // Try to load the cache file try {require $configPath; } catch (Throwable $e) { $this->files->delete($configPath); throw new LogicException('Your configuration files are not serializable.', 0, $e); $this->info('Configuration cached successfully! '); }Copy the code
  • PHP artisan config:clear

Illuminate\Foundation\Console\ConfigClearCommand

Public function handle() {$this->files->delete($this->laravel->getCachedConfigPath()); $this->info('Configuration cache cleared! '); }Copy the code

Pay attention

  1. PHP artisan config:clear, PHP artisan config:clear, PHP artisan config:clear, PHP artisan config:clear PHP artisan Config :cache
  2. The config content cannot be modified permanently

6, the end

I hope it will be helpful to you