1.1 Goto Your root project
cd /d/Projects/laravel70
1.2 Clear cache config
$ php artisan config:clear
Configuration cache cleared!
2.1 Read app.name
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ConfigController extends Controller
{
public function get()
{
var_dump(config('app.name'));
}
}
2.2 read config() from helper
vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
->
/**
* Get / set the specified configuration value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Config\Repository
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return app('config');
}
if (is_array($key)) {
return app('config')->set($key);
}
return app('config')->get($key, $default);
}
2.3 Get config object (\Illuminate\Config\Repository) from container
/**
* Get the available container instance.
*
* @param string|null $abstract
* @param array $parameters
* @return mixed|\Illuminate\Contracts\Foundation\Application
*/
function app($abstract = null, array $parameters = [])
{
if (is_null($abstract)) {
return Container::getInstance();
}
return Container::getInstance()
->make($abstract, $parameters);
}
2.4 resole Configuration obj
vendor/laravel/framework/src/Illuminate/Foundation/Application.php
->
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function make($abstract, array $parameters = [])
{
var_dump('application.make', $abstract, $parameters);
$this->loadDeferredProviderIfNeeded(
$abstract = $this->getAlias($abstract)
);
return parent::make($abstract, $parameters);
}
D:\Projects\laravel70\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:779:string 'application.make' (length=16)
D:\Projects\laravel70\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:779:string 'Illuminate\Foundation\Bootstrap\LoadConfiguration' (length=49)
D:\Projects\laravel70\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:779:
array (size=0)
2.5 Load Configuration
vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php
->
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
$items = [];
// First we will see if we have a cache configuration file. If we do, we'll load
// the configuration items from that file so that it is very quick. Otherwise
// we will need to spin through every configuration file and load them all.
if (file_exists($cached = $app->getCachedConfigPath())) {
$items = require $cached;
$loadedFromCache = true;
}
// Next we will spin through all of the configuration files in the configuration
// directory and load each one into the repository. This will make all of the
// options available to the developer for use in various parts of this app.
$app->instance('config', $config = new Repository($items));
if (! isset($loadedFromCache)) {
$this->loadConfigurationFiles($app, $config);
}
// Finally, we will set the application's environment based on the configuration
// values that were loaded. We will pass a callback which will be used to get
// the environment in a web context where an "--env" switch is not present.
$app->detectEnvironment(function () use ($config) {
return $config->get('app.env', 'production');
});
date_default_timezone_set($config->get('app.timezone', 'UTC'));
mb_internal_encoding('UTF-8');
}
2.6 Load config from file
vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php
-------->
/**
* Load the configuration items from all of the files.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $repository
* @return void
*
* @throws \Exception
*/
protected function loadConfigurationFiles(Application $app,
RepositoryContract $repository)
{
$files = $this->getConfigurationFiles($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);
}
}
2.7 Therefore all config is load from config files, no cached
3.1 Cached configs
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
3.2 Config cached file wil been generated
bootstrap/cache/config.php
3.3 When you call config, it will been read from cached file
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ConfigController extends Controller
{
public function get()
{
var_dump(config('app.name'));
}
}
3.4 Load config from cached
Review step 2.5 above
if (file_exists($cached = $app->getCachedConfigPath())) {
$items = require $cached;
$loadedFromCache = true;
}