一旦用户(浏览器)发送了一个http请求,我们的apache或者nginx一般都转到index.php,因此,之后的一系列步骤都是从index.php开始的,我们先来看一看这个文件代码。
<?phprequire __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 nd the associated respon back to| the client's browr allowing them to enjoy the creative| and wonderful application we have prepared for them.|*/$kernel = $app->make(illuminate\contracts\http\kernel::class);$respon = $kernel->handle( $request = illuminate\http\request::capture());$respon->nd();$kernel->terminate($request, $respon);
这里在注释里谈了kernel的作用,kernel处理来访的请求,并且发送相应返回给用户浏览器。
这里又涉及到了一个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 rves 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 interfaces18 |--------------------------------------------------------------------------19 |20 | next, we need to bind some important interfaces into the container so21 | we will be able to resolve them when needed. the kernels rve the22 | 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::class28 );29 $app->singleton(30 illuminate\cont韦庄racts\console\kernel::class,31 app\console\kernel::class32 );33 $app->singleton(34 illuminate\contracts\debug\exceptionhandler::class,35 app\exceptions\handler::class36 );37 /*38 |--------------------------------------------------------------------------39 | return the application40 |--------------------------------------------------------------------------41 |42 | this script returns the application instance. the instance is given to43 | the calling script so we can parate the building of the instances44 | from the actual running of the application and nding respons.45 |46 */47 return $app;
请看app变量是illuminate\foundation\application类的对象,所以调用了这个类的构造函数,具体做了什么事,我们看源码。
1 public function __construct($bapath = null)2 {3 if ($bapath) {4 $this->tbapath($bapath);5 }6 $this->registerbabindings();7 $this->registerbarviceproviders();8 $this->registercorecontaineralias();9 }
构造器做了3件事,前两件事很好理解,创建container,注册了rviceprovider,看代码
1 /** 2 * register the basic bindings into the container. 3 * 4 * @return void 5 */ 6 protected function registerbabindings() 7 { 8 static::tinstance($this); 9 $this->instance('app', $this);10 $this->instance(container::class, $this);11 }12 /**13 * register all of the ba rvice providers.14 *15 * @return void16 */17 protected function registerbarviceproviders()18 {19 $this->register(new eventrviceprovider($this));20 $this->register(new logrviceprovider($this));21 $this->register(new routingrviceprovider($this));22 }
最后一件事,是做了个很大的数组,定义了大量的别名,侧面体现程序员是聪明的懒人。
1 /** 2 * register the core class alias in the container. 3 * 4 * @return void 5 */ 6 public function registercorecontaineralias() 7 { 8 $alias = [ 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\databa\databamanager::class],19 'db.connection' => [\illuminate\databa\connection::class, \illuminate\databa\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\cont防汛条例racts\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 'ssion' => [\illuminate\ssion\ssionmanager::class],39 'ssion.store' => [\illuminate\ssion\store::class, \illuminate\contracts\ssion\ssion::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 ($alias as $key => $alias) {45 foreach ($alias as $alias) {46 $this->alias($key, $alias);47 }48 }49 }
这里出现了一个instance函数,其实这并不是application类的函数,而是application类的父类container类的函数
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 unt($this->alias[$abstract]);12 // we'll check to determine if this type has been bound before, and if it has13 // we will fire the rebound callbacks registered with the container and it14 // can be updated with consuming class 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类的源代码文件里查。
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 u illuminate\foundation\http\kernel as httpkernel; 4 class kernel extends httpkernel 5 { 6 /** 7 * the application's global http middleware stack. 8 * 9 * the middleware are run during every request to your application.10 *11 *三八妇女节贺卡制作 @var array12 */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 array21 */22 protected $middlewaregroups = [23 'web' => [24 \app\ht人体最大的器官tp\middleware\encryptcookies::class,25 \illuminate\cookie\middleware\addqueuedcookiestorespon::class,26 \illuminate\ssion\middleware\startssion::class,27 \illuminate\view\middleware\shareerrorsfromssion::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 * the middleware may be assigned to groups or ud individually.38 *39 * @var array40 */41 protected $routemiddleware = [42 'auth' => \app\htt碧潭幽谷p\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 $respon = $kernel->handle(2 $request = illuminate\http\request::capture()3 );4 $respon->nd();
最后在中止,释放所有资源。
1 /** 2 * call the terminate method on any terminable middleware. 3 * 4 * @param \illuminate\http\request $request 5 * @param \illuminate\http\respon $respon 6 * @return void 7 */ 8 public function terminate($request, $respon) 9 {10 $this->terminatemiddleware($request, $respon);11 $this->app->terminate();12 }
总结一下,简单归纳整个过程就是:
1.index.php加载\bootstrap\app.php,在application类的构造函数中创建container,注册了rviceprovider,定义了别名数组,然后用app变量保存构造函数构造出来的对象。
2.使用app这个对象,创建1个单例模式的对象httpkernel,在创建httpkernel时调用了构造函数,完成了中间件的声明。
3.以上这些工作都是在请求来访之前完成的,接下来开始等待请求,然后就是:接受到请求–>处理请求–>发送响应–>中止app变量
本文发布于:2023-04-07 21:51:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/871bb94d2e0f9fcb1b6629dd16e20ad7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Laravel生命周期与原理.doc
本文 PDF 下载地址:Laravel生命周期与原理.pdf
留言与评论(共有 0 条评论) |