/public/index.php contains a line of code that initializes the server container. The related file is: /bootstrap/app.php
Code: the bootstrap/app. PHP
<? $app = new Illuminate\Foundation\Application(realPath (__DIR__.'/.. / ')); // singleton an App\Http\Kernel object, $App ->singleton('Illuminate\Contracts\Http\Kernel') can be called using App::make('Illuminate\Contracts\Http\Kernel'). 'App\Http\Kernel' ); // singleton an App Console Kernel object, $App ->singleton('Illuminate\Contracts\Console\Kernel') can be called using App::make('Illuminate\Contracts\Console\Kernel'). 'App\Console\Kernel' ); $app->singleton('Illuminate\Contracts\Debug\ExceptionHandler', 'app \Exceptions\Handler'); Return $app;Copy the code
Code: Illuminate, Foundation and Application
// Too much code to explain only a few major methods (truth is, I don't know much, Public function __construct($basePath = null) {$this->registerBaseBindings(); / / registered in the container of the most basic service providers (i.e. the ServiceProvider) $this - > registerBaseServiceProviders (); / / registered in container some core class alias (this view seems a bit wrong, can see the following code comments to understand) $this - > registerCoreContainerAliases (); If ($basePath) $this->setBasePath($basePath); } protected function registerBaseBindings() {// Initialize an empty container static::setInstance($this); $this->instance('app', $this); $this->instance('app', $this); $this->instance('Illuminate\Container\Container', $this); } protected function registerBaseServiceProviders () {/ / EventServiceProvider the service provider, it is to register for a key container object for the events, $this-> Register (new EventServiceProvider($this)); $this->register(new) $this->register(new) $this->register(new RoutingServiceProvider($this)); } /* We want to get an instance of the container before we call this method App::make(' App '); Now we can use App::make('Illuminate\Foundation\Application') App::make('Illuminate\Contracts\Container\Container') App::make('Illuminate\Contracts\Foundation\Application') three ways to get a container instance, Illuminate\Foundation\Application, Illuminate\Contracts\Container\Container, Illuminate\Contracts\Foundation\Application 3 Are aliases for app; */ public function registerCoreContainerAliases() { $aliases = array( 'app' => ['Illuminate\Foundation\Application', 'Illuminate\Contracts\Container\Container', 'Illuminate\Contracts\Foundation\Application'], 'artisan' => ['Illuminate\Console\Application', 'Illuminate\Contracts\Console\Application'], 'auth' => 'Illuminate\Auth\AuthManager', 'auth.driver' => ['Illuminate\Auth\Guard', 'Illuminate\Contracts\Auth\Guard'], 'auth.password.tokens' => 'Illuminate\Auth\Passwords\TokenRepositoryInterface', 'blade.compiler' => 'Illuminate\View\Compilers\BladeCompiler', 'cache' => ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'], 'cache.store' => ['Illuminate\Cache\Repository', 'Illuminate\Contracts\Cache\Repository'], 'config' => ['Illuminate\Config\Repository', 'Illuminate\Contracts\Config\Repository'], 'cookie' => ['Illuminate\Cookie\CookieJar', 'Illuminate\Contracts\Cookie\Factory', 'Illuminate\Contracts\Cookie\QueueingFactory'], 'encrypter' => ['Illuminate\Encryption\Encrypter', 'Illuminate\Contracts\Encryption\Encrypter'], 'db' => 'Illuminate\Database\DatabaseManager', 'events' => ['Illuminate\Events\Dispatcher', 'Illuminate\Contracts\Events\Dispatcher'], 'files' => 'Illuminate\Filesystem\Filesystem', 'filesystem' => 'Illuminate\Contracts\Filesystem\Factory', 'filesystem.disk' => 'Illuminate\Contracts\Filesystem\Filesystem', 'filesystem.cloud' => 'Illuminate\Contracts\Filesystem\Cloud', 'hash' => 'Illuminate\Contracts\Hashing\Hasher', 'translator' => ['Illuminate\Translation\Translator', 'Symfony\Component\Translation\TranslatorInterface'], 'log' => ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'], 'mailer' => ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'], 'paginator' => 'Illuminate\Pagination\Factory', 'auth.password' => ['Illuminate\Auth\Passwords\PasswordBroker', 'Illuminate\Contracts\Auth\PasswordBroker'], 'queue' => ['Illuminate\Queue\QueueManager', 'Illuminate\Contracts\Queue\Factory', 'Illuminate\Contracts\Queue\Monitor'], 'queue.connection' => 'Illuminate\Contracts\Queue\Queue', 'redirect' => 'Illuminate\Routing\Redirector', 'redis' => ['Illuminate\Redis\Database', 'Illuminate\Contracts\Redis\Database'], 'request' => 'Illuminate\Http\Request', 'router' => ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar'], 'session' => 'Illuminate\Session\SessionManager', 'session.store' => ['Illuminate\Session\Store', 'Symfony\Component\HttpFoundation\Session\SessionInterface'], 'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'], 'validator' => ['Illuminate\Validation\Factory', 'Illuminate\Contracts\Validation\Factory'], 'view' => ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'], ); foreach ($aliases as $key => $aliases) { foreach ((array) $aliases as $alias) { $this->alias($key, $alias); }}}Copy the code
The resulting container instance
Application {# 2 # ▼ basePath: "/ Applications/XAMPP/xamppfiles/htdocs/laravel" # hasBeenBootstrapped: false # booted: false #bootingCallbacks: [] #bootedCallbacks: [] #terminatingCallbacks: [] #serviceProviders: [] #storagePath: null #environmentFile: ". Env "#resolved: Array: 1 [▶] # bindings: Array: 8 [▼ "events" = > array: 2 [▶] "router" = > array: 2 [▶] "url" = > array: 2 [▶] "redirect" = > array: 2 [▶] "Illuminate\Contracts\Routing\ResponseFactory" => array:2 [▶] "Illuminate\Contracts\Http\Kernel" => array:2 [▶] "Illuminate\Contracts\Console\Kernel" => array:2 [▶] "Illuminate\Contracts\Debug\ExceptionHandler" => array:2 [▶]] #instances: Array :10 [▼ "app" => Application {#2} "Illuminate\Container\Container" => Application {#2} "events" => Dispatcher {#5 ▶} "path" => "/Applications/XAMPP/xamppfiles/htdocs/laravel/app" "path.base" => "/Applications/XAMPP/xamppfiles/htdocs/laravel" "path.config" => "/Applications/XAMPP/xamppfiles/htdocs/laravel/config" "path.database" => "/Applications/XAMPP/xamppfiles/htdocs/laravel/database" "path.lang" => "/Applications/XAMPP/xamppfiles/htdocs/laravel/resources/lang" "path.public" => "/Applications/XAMPP/xamppfiles/htdocs/laravel/public" "path.storage" => "/Applications/XAMPP/xamppfiles/htdocs/laravel/storage" ] #aliases: Array :59 [▼ "Illuminate\Foundation\Application" => "app" "Illuminate\Foundation\ Container\Container" => "app" "Illuminate\Contracts\Foundation\Application" => "app" "Illuminate\Console\Application" => "artisan" "Illuminate\Contracts\Console\Application" => "artisan" "Illuminate\Auth\AuthManager" => "auth" "Illuminate\Auth\Guard" => "auth.driver" "Illuminate\Contracts\Auth\Guard" => "auth.driver" "Illuminate\Auth\Passwords\TokenRepositoryInterface" => "auth.password.tokens" "Illuminate\View\Compilers\BladeCompiler" => "blade.compiler" "Illuminate\Cache\CacheManager" => "cache" "Illuminate\Contracts\Cache\Factory" => "cache" "Illuminate\Cache\Repository" => "cache.store" "Illuminate\Contracts\Cache\Repository" => "cache.store" "Illuminate\Config\Repository" => "config" "Illuminate\Contracts\Config\Repository" => "config" "Illuminate\Cookie\CookieJar" => "cookie" "Illuminate\Contracts\Cookie\Factory" => "cookie" "Illuminate\Contracts\Cookie\QueueingFactory" => "cookie" "Illuminate\Encryption\Encrypter" => "encrypter" "Illuminate\Contracts\Encryption\Encrypter" => "encrypter" "Illuminate\Database\DatabaseManager" => "db" "Illuminate\Events\Dispatcher" => "events" "Illuminate\Contracts\Events\Dispatcher" => "events" "Illuminate\Filesystem\Filesystem" => "files" "Illuminate\Contracts\Filesystem\Factory" => "filesystem" "Illuminate\Contracts\Filesystem\Filesystem" => "filesystem.disk" "Illuminate\Contracts\Filesystem\Cloud" => "filesystem.cloud" "Illuminate\Contracts\Hashing\Hasher" => "hash" "Illuminate\Translation\Translator" => "translator" "Symfony\Component\Translation\TranslatorInterface" => "translator" "Illuminate\Log\Writer" => "log" "Illuminate\Contracts\Logging\Log" => "log" "Psr\Log\LoggerInterface" => "log" "Illuminate\Mail\Mailer" => "mailer" "Illuminate\Contracts\Mail\Mailer" => "mailer" "Illuminate\Contracts\Mail\MailQueue" => "mailer" "Illuminate\Pagination\Factory" => "paginator" "Illuminate\Auth\Passwords\PasswordBroker" => "auth.password" "Illuminate\Contracts\Auth\PasswordBroker" => "auth.password" "Illuminate\Queue\QueueManager" => "queue" "Illuminate\Contracts\Queue\Factory" => "queue" "Illuminate\Contracts\Queue\Monitor" => "queue" "Illuminate\Contracts\Queue\Queue" => "queue.connection" "Illuminate\Routing\Redirector" => "redirect" "Illuminate\Redis\Database" => "redis" "Illuminate\Contracts\Redis\Database" => "redis" "Illuminate\Http\Request" => "request" "Illuminate\Routing\Router" => "router" "Illuminate\Contracts\Routing\Registrar" => "router" "Illuminate\Session\SessionManager" => "session" "Illuminate\Session\Store" => "session.store" "Symfony\Component\HttpFoundation\Session\SessionInterface" => "session.store" "Illuminate\Routing\UrlGenerator" => "url" "Illuminate\Contracts\Routing\UrlGenerator" => "url" "Illuminate\Validation\Factory" => "validator" "Illuminate\Contracts\Validation\Factory" => "validator" "Illuminate\View\Factory" => "view" "Illuminate\Contracts\View\Factory" => "view" ] #extenders: [] #tags: [] #buildStack: [] +contextual: [] #reboundCallbacks: [] #globalResolvingCallbacks: [] #globalAfterResolvingCallbacks: [] #resolvingCallbacks: [] #afterResolvingCallbacks: [] }Copy the code
How w to print an instance? So at this point, you can do dd(app())
Dd (app())? There are two methods dd() and app(), which are defined in the fourth method, auto-loading
App::make(‘ App ‘) This is because this method needs to use Contracts, and at this point App is not defined as an alias for Illuminate\Support\Facades\App and therefore cannot be used;
The handle method that runs the Kernel class in the unified entry file can only be used, so it can be used in the Controller, but cannot be used now.