Laravel生命週期與原理


一旦用戶(瀏覽器)發送了一個HTTP請求,咱們的apache或者nginx通常都轉到index.php,所以,以後的一系列步驟都是從index.php開始的,咱們先來看一看這個文件代碼。php

<?php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
  $request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);

 

這裏在註釋裏談了kernel的做用,kernel處理來訪的請求,而且發送相應返回給用戶瀏覽器。nginx

這裏又涉及到了一個app對象,因此附上app對象的源碼,這份源碼是\bootstrap\app.php程序員

 1 <?php
 2 /*
 3 |--------------------------------------------------------------------------
 4 | Create The Application
 5 |--------------------------------------------------------------------------
 6 |
 7 | The first thing we will do is create a new Laravel application instance
 8 | which serves as the "glue" for all the components of Laravel, and is
 9 | the IoC container for the system binding all of the various parts.
10 |
11 */
12 $app = new Illuminate\Foundation\Application(
13   realpath(__DIR__.'/../')
14 );
15 /*
16 |--------------------------------------------------------------------------
17 | Bind Important Interfaces
18 |--------------------------------------------------------------------------
19 |
20 | Next, we need to bind some important interfaces into the container so
21 | we will be able to resolve them when needed. The kernels serve the
22 | incoming requests to this application from both the web and CLI.
23 |
24 */
25 $app->singleton(
26   Illuminate\Contracts\Http\Kernel::class,
27   App\Http\Kernel::class
28 );
29 $app->singleton(
30   Illuminate\Contracts\Console\Kernel::class,
31   App\Console\Kernel::class
32 );
33 $app->singleton(
34   Illuminate\Contracts\Debug\ExceptionHandler::class,
35   App\Exceptions\Handler::class
36 );
37 /*
38 |--------------------------------------------------------------------------
39 | Return The Application
40 |--------------------------------------------------------------------------
41 |
42 | This script returns the application instance. The instance is given to
43 | the calling script so we can separate the building of the instances
44 | from the actual running of the application and sending responses.
45 |
46 */
47 return $app;

 

 

請看app變量是Illuminate\Foundation\Application類的對象,因此調用了這個類的構造函數,具體作了什麼事,咱們看源碼。web

1 public function __construct($basePath = null)
2 {
3   if ($basePath) {
4     $this->setBasePath($basePath);
5   }
6   $this->registerBaseBindings();
7   $this->registerBaseServiceProviders();
8   $this->registerCoreContainerAliases();
9 }

 

構造器作了3件事,前兩件事很好理解,建立Container,註冊了ServiceProvider,看代碼redis

 1 /**
 2  * Register the basic bindings into the container.
 3  *
 4  * @return void
 5  */
 6 protected function registerBaseBindings()
 7 {
 8   static::setInstance($this);
 9   $this->instance('app', $this);
10   $this->instance(Container::class, $this);
11 }
12 /**
13  * Register all of the base service providers.
14  *
15  * @return void
16  */
17 protected function registerBaseServiceProviders()
18 {
19   $this->register(new EventServiceProvider($this));
20   $this->register(new LogServiceProvider($this));
21   $this->register(new RoutingServiceProvider($this));
22 }

 

最後一件事,是作了個很大的數組,定義了大量的別名,側面體現程序員是聰明的懶人。apache

 1 /**
 2  * Register the core class aliases in the container.
 3  *
 4  * @return void
 5  */
 6 public function registerCoreContainerAliases()
 7 {
 8   $aliases = [
 9     'app'         => [\Illuminate\Foundation\Application::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class],
10     'auth'         => [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class],
11     'auth.driver'     => [\Illuminate\Contracts\Auth\Guard::class],
12     'blade.compiler'    => [\Illuminate\View\Compilers\BladeCompiler::class],
13     'cache'        => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class],
14     'cache.store'     => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class],
15     'config'        => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class],
16     'cookie'        => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class],
17     'encrypter'      => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class],
18     'db'          => [\Illuminate\Database\DatabaseManager::class],
19     'db.connection'    => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
20     'events'        => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
21     'files'        => [\Illuminate\Filesystem\Filesystem::class],
22     'filesystem'      => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class],
23     'filesystem.disk'   => [\Illuminate\Contracts\Filesystem\Filesystem::class],
24     'filesystem.cloud'   => [\Illuminate\Contracts\Filesystem\Cloud::class],
25     'hash'         => [\Illuminate\Contracts\Hashing\Hasher::class],
26     'translator'      => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class],
27     'log'         => [\Illuminate\Log\Writer::class, \Illuminate\Contracts\Logging\Log::class, \Psr\Log\LoggerInterface::class],
28     'mailer'        => [\Illuminate\Mail\Mailer::class, \Illuminate\Contracts\Mail\Mailer::class, \Illuminate\Contracts\Mail\MailQueue::class],
29     'auth.password'    => [\Illuminate\Auth\Passwords\PasswordBrokerManager::class, \Illuminate\Contracts\Auth\PasswordBrokerFactory::class],
30     'auth.password.broker' => [\Illuminate\Auth\Passwords\PasswordBroker::class, \Illuminate\Contracts\Auth\PasswordBroker::class],
31     'queue'        => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class],
32     'queue.connection'   => [\Illuminate\Contracts\Queue\Queue::class],
33     'queue.failer'     => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class],
34     'redirect'       => [\Illuminate\Routing\Redirector::class],
35     'redis'        => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class],
36     'request'       => [\Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class],
37     'router'        => [\Illuminate\Routing\Router::class, \Illuminate\Contracts\Routing\Registrar::class, \Illuminate\Contracts\Routing\BindingRegistrar::class],
38     'session'       => [\Illuminate\Session\SessionManager::class],
39     'session.store'    => [\Illuminate\Session\Store::class, \Illuminate\Contracts\Session\Session::class],
40     'url'         => [\Illuminate\Routing\UrlGenerator::class, \Illuminate\Contracts\Routing\UrlGenerator::class],
41     'validator'      => [\Illuminate\Validation\Factory::class, \Illuminate\Contracts\Validation\Factory::class],
42     'view'         => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class],
43   ];
44   foreach ($aliases as $key => $aliases) {
45     foreach ($aliases as $alias) {
46       $this->alias($key, $alias);
47     }
48   }
49 }

 

 

這裏出現了一個instance函數,其實這並非Application類的函數,而是Application類的父類Container類的函數bootstrap

 1 /**
 2  * Register an existing instance as shared in the container.
 3  *
 4  * @param string $abstract
 5  * @param mixed  $instance
 6  * @return void
 7  */
 8 public function instance($abstract, $instance)
 9 {
10   $this->removeAbstractAlias($abstract);
11   unset($this->aliases[$abstract]);
12   // We'll check to determine if this type has been bound before, and if it has
13   // we will fire the rebound callbacks registered with the container and it
14   // can be updated with consuming classes that have gotten resolved here.
15   $this->instances[$abstract] = $instance;
16   if ($this->bound($abstract)) {
17     $this->rebound($abstract);
18   }
19 }

 

Application是Container的子類,因此$app不只是Application類的對象,仍是Container的對象,因此,新出現的singleton函數咱們就能夠到Container類的源代碼文件裏查。api

singleton這個函數,前一個參數是實際類名,後一個參數是類的「別名」。數組

$app對象聲明瞭3個單例模型對象,分別是HttpKernel,ConsoleKernel,ExceptionHandler。請注意,這裏並無建立對象,只是聲明,也只是起了一個「別名」。瀏覽器

 

你們有沒有發現,index.php中也有一個$kernel變量,可是隻保存了make出來的HttpKernel變量,所以本文再也不討論,ConsoleKernel,ExceptionHandler。。。

繼續在文件夾下找到App\Http\Kernel.php,既然咱們把實際的HttpKernel作的事情都寫在這個php文件裏,就從這份代碼裏看看究竟作了哪些事?

 1 <?php
 2 namespace App\Http;
 3 use Illuminate\Foundation\Http\Kernel as HttpKernel;
 4 class Kernel extends HttpKernel
 5 {
 6   /**
 7    * The application's global HTTP middleware stack.
 8    *
 9    * These middleware are run during every request to your application.
10    *
11    * @var array
12    */
13   protected $middleware = [
14     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
15     //\App\Http\Middleware\MyMiddleware::class,
16   ];
17   /**
18    * The application's route middleware groups.
19    *
20    * @var array
21    */
22   protected $middlewareGroups = [
23     'web' => [
24       \App\Http\Middleware\EncryptCookies::class,
25       \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
26       \Illuminate\Session\Middleware\StartSession::class,
27       \Illuminate\View\Middleware\ShareErrorsFromSession::class,
28       \App\Http\Middleware\VerifyCsrfToken::class,
29     ],
30     'api' => [
31       'throttle:60,1',
32     ],
33   ];
34   /**
35    * The application's route middleware.
36    *
37    * These middleware may be assigned to groups or used individually.
38    *
39    * @var array
40    */
41   protected $routeMiddleware = [
42     'auth' => \App\Http\Middleware\Authenticate::class,
43     'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
44     'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
45     'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
46   'mymiddleware'=>\App\Http\Middleware\MyMiddleware::class,
47   ];
48 }

 

 

一目瞭然,HttpKernel裏定義了中間件數組。

該作的作完了,就開始了請求到響應的過程,見index.php

1 $response = $kernel->handle(
2   $request = Illuminate\Http\Request::capture()
3 );
4 $response->send();

 

最後在停止,釋放全部資源。

 1 /**
 2 * Call the terminate method on any terminable middleware.
 3 *
 4 * @param \Illuminate\Http\Request $request
 5 * @param \Illuminate\Http\Response $response
 6 * @return void
 7 */
 8 public function terminate($request, $response)
 9 {
10     $this->terminateMiddleware($request, $response);
11     $this->app->terminate();
12 }

 

總結一下,簡單概括整個過程就是:

1.index.php加載\bootstrap\app.php,在Application類的構造函數中建立Container,註冊了ServiceProvider,定義了別名數組,而後用app變量保存構造函數構造出來的對象。

2.使用app這個對象,建立1個單例模式的對象HttpKernel,在建立HttpKernel時調用了構造函數,完成了中間件的聲明。

3.以上這些工做都是在請求來訪以前完成的,接下來開始等待請求,而後就是:接受到請求-->處理請求-->發送響應-->停止app變量

相關文章
相關標籤/搜索