Laravel框架生命周期与原理分析
本文实例讲述了laravel框架生命周期与原理。分享给大家供大家参考,具体如下:
引言:
如果你对一件工具的使用原理了如指掌,那么你在用这件工具的时候会充满信心!
正文:
一旦用户(浏览器)发送了一个http请求,我们的apache或者nginx一般都转到index.php,因此,之后的一系列步骤都是从index.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的作用,kernel处理来访的请求,并且发送相应返回给用户浏览器。
这里又涉及到了一个app对象,所以附上app对象,所以附上app对象的源码,这份源码是\bootstrap\app.php
<?php /* |-------------------------------------------------------------------------- | create the application |-------------------------------------------------------------------------- | | the first thing we will do is create a new laravel application instance | which serves as the "glue" for all the components of laravel, and is | the ioc container for the system binding all of the various parts. | */ $app = new illuminate\foundation\application( realpath(__dir__.'/../') ); /* |-------------------------------------------------------------------------- | bind important interfaces |-------------------------------------------------------------------------- | | next, we need to bind some important interfaces into the container so | we will be able to resolve them when needed. the kernels serve the | incoming requests to this application from both the web and cli. | */ $app->singleton( illuminate\contracts\http\kernel::class, app\http\kernel::class ); $app->singleton( illuminate\contracts\console\kernel::class, app\console\kernel::class ); $app->singleton( illuminate\contracts\debug\exceptionhandler::class, app\exceptions\handler::class ); /* |-------------------------------------------------------------------------- | return the application |-------------------------------------------------------------------------- | | this script returns the application instance. the instance is given to | the calling script so we can separate the building of the instances | from the actual running of the application and sending responses. | */ return $app;
请看app变量是illuminate\foundation\application类的对象,所以调用了这个类的构造函数,具体做了什么事,我们看源码。
public function __construct($basepath = null) { if ($basepath) { $this->setbasepath($basepath); } $this->registerbasebindings(); $this->registerbaseserviceproviders(); $this->registercorecontaineraliases(); }
构造器做了3件事,前两件事很好理解,创建container,注册了serviceprovider,看代码
/** * register the basic bindings into the container. * * @return void */ protected function registerbasebindings() { static::setinstance($this); $this->instance('app', $this); $this->instance(container::class, $this); } /** * register all of the base service providers. * * @return void */ protected function registerbaseserviceproviders() { $this->register(new eventserviceprovider($this)); $this->register(new logserviceprovider($this)); $this->register(new routingserviceprovider($this)); }
最后一件事,是做了个很大的数组,定义了大量的别名,侧面体现程序员是聪明的懒人。
/** * register the core class aliases in the container. * * @return void */ public function registercorecontaineraliases() { $aliases = [ 'app' => [\illuminate\foundation\application::class, \illuminate\contracts\container\container::class, \illuminate\contracts\foundation\application::class], 'auth' => [\illuminate\auth\authmanager::class, \illuminate\contracts\auth\factory::class], 'auth.driver' => [\illuminate\contracts\auth\guard::class], 'blade.compiler' => [\illuminate\view\compilers\bladecompiler::class], 'cache' => [\illuminate\cache\cachemanager::class, \illuminate\contracts\cache\factory::class], 'cache.store' => [\illuminate\cache\repository::class, \illuminate\contracts\cache\repository::class], 'config' => [\illuminate\config\repository::class, \illuminate\contracts\config\repository::class], 'cookie' => [\illuminate\cookie\cookiejar::class, \illuminate\contracts\cookie\factory::class, \illuminate\contracts\cookie\queueingfactory::class], 'encrypter' => [\illuminate\encryption\encrypter::class, \illuminate\contracts\encryption\encrypter::class], 'db' => [\illuminate\database\databasemanager::class], 'db.connection' => [\illuminate\database\connection::class, \illuminate\database\connectioninterface::class], 'events' => [\illuminate\events\dispatcher::class, \illuminate\contracts\events\dispatcher::class], 'files' => [\illuminate\filesystem\filesystem::class], 'filesystem' => [\illuminate\filesystem\filesystemmanager::class, \illuminate\contracts\filesystem\factory::class], 'filesystem.disk' => [\illuminate\contracts\filesystem\filesystem::class], 'filesystem.cloud' => [\illuminate\contracts\filesystem\cloud::class], 'hash' => [\illuminate\contracts\hashing\hasher::class], 'translator' => [\illuminate\translation\translator::class, \illuminate\contracts\translation\translator::class], 'log' => [\illuminate\log\writer::class, \illuminate\contracts\logging\log::class, \psr\log\loggerinterface::class], 'mailer' => [\illuminate\mail\mailer::class, \illuminate\contracts\mail\mailer::class, \illuminate\contracts\mail\mailqueue::class], 'auth.password' => [\illuminate\auth\passwords\passwordbrokermanager::class, \illuminate\contracts\auth\passwordbrokerfactory::class], 'auth.password.broker' => [\illuminate\auth\passwords\passwordbroker::class, \illuminate\contracts\auth\passwordbroker::class], 'queue' => [\illuminate\queue\queuemanager::class, \illuminate\contracts\queue\factory::class, \illuminate\contracts\queue\monitor::class], 'queue.connection' => [\illuminate\contracts\queue\queue::class], 'queue.failer' => [\illuminate\queue\failed\failedjobproviderinterface::class], 'redirect' => [\illuminate\routing\redirector::class], 'redis' => [\illuminate\redis\redismanager::class, \illuminate\contracts\redis\factory::class], 'request' => [\illuminate\http\request::class, \symfony\component\httpfoundation\request::class], 'router' => [\illuminate\routing\router::class, \illuminate\contracts\routing\registrar::class, \illuminate\contracts\routing\bindingregistrar::class], 'session' => [\illuminate\session\sessionmanager::class], 'session.store' => [\illuminate\session\store::class, \illuminate\contracts\session\session::class], 'url' => [\illuminate\routing\urlgenerator::class, \illuminate\contracts\routing\urlgenerator::class], 'validator' => [\illuminate\validation\factory::class, \illuminate\contracts\validation\factory::class], 'view' => [\illuminate\view\factory::class, \illuminate\contracts\view\factory::class], ]; foreach ($aliases as $key => $aliases) { foreach ($aliases as $alias) { $this->alias($key, $alias); } } }
这里出现了一个instance函数,其实这并不是application类的函数,而是application类的父类container类的函数
/** * register an existing instance as shared in the container. * * @param string $abstract * @param mixed $instance * @return void */ public function instance($abstract, $instance) { $this->removeabstractalias($abstract); unset($this->aliases[$abstract]); // we'll check to determine if this type has been bound before, and if it has // we will fire the rebound callbacks registered with the container and it // can be updated with consuming classes that have gotten resolved here. $this->instances[$abstract] = $instance; if ($this->bound($abstract)) { $this->rebound($abstract); } }
application是container的子类,所以$app
不仅是application类的对象,还是container的对象,所以,新出现的singleton函数我们就可以到container类的源代码文件里查。bind函数和singleton的区别见这篇博文。
singleton这个函数,前一个参数是实际类名,后一个参数是类的“别名”。
$app
对象声明了3个单例模型对象,分别是httpkernel,consolekernel,exceptionhandler。请注意,这里并没有创建对象,只是声明,也只是起了一个“别名”。
大家有没有发现,index.php中也有一个$kernel变量,但是只保存了make出来的httpkernel变量,因此本文不再讨论,consolekernel,exceptionhandler。。。
继续在文件夹下找到app\http\kernel.php,既然我们把实际的httpkernel做的事情都写在这个php文件里,就从这份代码里看看究竟做了哪些事?
<?php namespace app\http; use illuminate\foundation\http\kernel as httpkernel; class kernel extends httpkernel { /** * the application's global http middleware stack. * * these middleware are run during every request to your application. * * @var array */ protected $middleware = [ \illuminate\foundation\http\middleware\checkformaintenancemode::class, //\app\http\middleware\mymiddleware::class, ]; /** * the application's route middleware groups. * * @var array */ protected $middlewaregroups = [ 'web' => [ \app\http\middleware\encryptcookies::class, \illuminate\cookie\middleware\addqueuedcookiestoresponse::class, \illuminate\session\middleware\startsession::class, \illuminate\view\middleware\shareerrorsfromsession::class, \app\http\middleware\verifycsrftoken::class, ], 'api' => [ 'throttle:60,1', ], ]; /** * the application's route middleware. * * these middleware may be assigned to groups or used individually. * * @var array */ protected $routemiddleware = [ 'auth' => \app\http\middleware\authenticate::class, 'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class, 'guest' => \app\http\middleware\redirectifauthenticated::class, 'throttle' => \illuminate\routing\middleware\throttlerequests::class, 'mymiddleware'=>\app\http\middleware\mymiddleware::class, ]; }
一目了然,httpkernel里定义了中间件数组。
该做的做完了,就开始了请求到响应的过程,见index.php
$response = $kernel->handle( $request = illuminate\http\request::capture() ); $response->send();
最后在中止,释放所有资源。
/** * call the terminate method on any terminable middleware. * * @param \illuminate\http\request $request * @param \illuminate\http\response $response * @return void */ public function terminate($request, $response) { $this->terminatemiddleware($request, $response); $this->app->terminate(); }
总结一下,简单归纳整个过程就是:
1.index.php加载\bootstrap\app.php,在application类的构造函数中创建container,注册了serviceprovider,定义了别名数组,然后用app变量保存构造函数构造出来的对象。
2.使用app这个对象,创建1个单例模式的对象httpkernel,在创建httpkernel时调用了构造函数,完成了中间件的声明。
3.以上这些工作都是在请求来访之前完成的,接下来开始等待请求,然后就是:接受到请求-->处理请求-->发送响应-->中止app变量
更多关于laravel相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于laravel框架的php程序设计有所帮助。