先来张图大致理解下laravel的生命周期。

下面对应相应的代码,解释上图。

//文件路径:laravel/public/index.php

/**
* laravel的启动时间
*/
define('LARAVEL_START', microtime(true)); /**
* 加载项目依赖。
* 现代PHP依赖于Composer包管理器,入口文件通过引入由Composer包管理器。
* 自动生成的类加载程序,可以轻松注册并加载所依赖的第三方组件库。
*/
require __DIR__.'/../vendor/autoload.php'; /**
* 创建laravel应用实例。
*/
$app = require_once __DIR__.'/../bootstrap/app.php'; // 接受请求并响应
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
); // 结束请求,进行回调
$response->send(); // 终止程序
$kernel->terminate($request, $response);
//文件路径:laravel/boostrap/app.php

//第一部分:创建应用实例
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
); //第二部分:完成内核绑定
$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 $app;
文件路径:laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php

class Kernel implements KernelContract
{
protected $bootstrappers = [
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, // 注册系统环境配置
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class, // 注册系统配置
\Illuminate\Foundation\Bootstrap\HandleExceptions::class, // 注册异常注册
\Illuminate\Foundation\Bootstrap\RegisterFacades::class, // 注册门面模式
\Illuminate\Foundation\Bootstrap\RegisterProviders::class, // 注册服务提供者
\Illuminate\Foundation\Bootstrap\BootProviders::class, // 注册服务提供者boot
]; // 处理请求
public function handle($request)
{
try {
$request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request);
} catch (Exception $e) {
$this->reportException($e); $response = $this->renderException($request, $e);
} catch (Throwable $e) {
$this->reportException($e = new FatalThrowableError($e)); $response = $this->renderException($request, $e);
} $this->app['events']->dispatch(
new Events\RequestHandled($request, $response)
); return $response;
} protected function sendRequestThroughRouter($request)
{
//一、将$request实例注册到APP容器
$this->app->instance('request', $request); //二、清除之前的$request实例缓存
Facade::clearResolvedInstance('request'); //三、启动引导程序
$this->bootstrap(); //四、发送请求
return (new Pipeline($this->app)) //创建管道
->send($request) //发送请求
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) //通过中间件
->then($this->dispatchToRouter()); //分发到路由
} // 启动引导程序
public function bootstrap()
{
if (! $this->app->hasBeenBootstrapped()) {
$this->app->bootstrapWith($this->bootstrappers());
}
} // 路由分发
protected function dispatchToRouter()
{
return function ($request) {
$this->app->instance('request', $request); return $this->router->dispatch($request);
};
} // 终止程序
public function terminate($request, $response)
{
$this->terminateMiddleware($request, $response); $this->app->terminate();
}

图解Laravel的生命周期的更多相关文章

  1. 图解ios程序生命周期

    图解ios程序生命周期 应用程序启动后状态有Active.Inactive.Background.Suspended.Not running这5种状态,几种状态的转换见下图: 在AppDelegate ...

  2. Laravel的生命周期

    当你使用一个工具的时候, 如果你对这个工具的内部原理和构造有所了解, 那么在使用这个工具的时候, 就会更加的有信心, 工具用起来也会更加的得心应手. 今天阅读了 Laravel 官方的生命周期文档.这 ...

  3. 我所理解的 Laravel 请求 生命周期

    转载自:https://laravel-china.org/topics/3343/my-understanding-of-the-laravel-request-life-cycle 当你使用一个工 ...

  4. 图解:Activity生命周期

    当用户需要对手机通过屏幕进行交互时,比如打一个电话,拍张照片,发送一个邮件,或者查看地图.开发者就需要实现一个活动(Activity).每个活动都将作为一个提供用户使用接口的窗口.它可以填满整个屏幕, ...

  5. 附实例!图解React的生命周期及执行顺序

    本文由云+社区发表 作者:前端林子 1.七个可选的生命周期 可以结合下图来看: (1) componentWillMount() 仅在render()方法前被调用一次,如果在该方法中调用了setSta ...

  6. 图解django的生命周期

    其实django的生命周期的大体框架就是这样,剩下的细致的,自己再补充! 图片实在是有点抽象! 谅解!! koala-----给你更多技术小干货

  7. Laravel源码分析--Laravel生命周期详解

    一.XDEBUG调试 这里我们需要用到php的 xdebug 拓展,所以需要小伙伴们自己去装一下,因为我这里用的是docker,所以就简单介绍下在docker中使用xdebug的注意点. 1.在php ...

  8. laravel生命周期和核心思想

    工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...

  9. 微信小程序-生命周期图解

    微信小程序-生命周期图解 小程序生命周期 App 生命周期 https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.htm ...

随机推荐

  1. SPFA 算法(剪辑)(学习!)

    SPFA算法 单源最短路径的算法最常用的是Dijkstra,些算法从时间复杂度来说为O(n^2),但是面对含有负权植的图来说就无能为力了,此时 Dellman-ford算法就有用了,这咱算法是采用的是 ...

  2. linux安全相关

    2017-05-11突然谈到linux安全相关的话题,记录一下 搜了一下,找到一篇介绍apparmor和selinux的文章 http://www.361way.com/apparmor-selinu ...

  3. C++拷贝构造函数(深拷贝,浅拷贝)

    http://www.cnblogs.com/BlueTzar/articles/1223313.html C++拷贝构造函数(深拷贝,浅拷贝) 对于普通类型的对象来说,它们之间的复制是很简单的,例如 ...

  4. ubuntu commands mysql

    use table; //选择某个表 show tables; //展现有几个表 select * from table; //显示表内容

  5. NTFS文件系统的主要优点体现在以下三个方面

    1.NFTS格式具有用户权限的控制管理功能 2.NTFS有更好的压缩率 3.NTFS具有日志功能,可以提高系统安全性

  6. ReactJS结合ES6入门Template

    一.前言 二.介绍 ReactJS ECMAScript 6 三.入门DEMO "Hello,XXX“ 输出 ES5写法 <div id="example"> ...

  7. GitHub基本使用

    什么是GitHub? GitHub是用于版本控制和协作的代码托管平台.它可以让您和其他人在任何地方一起工作 本教程教你如GitHub必需资源,如仓库,分支,提交和拉请求.您将创建自己的Hello Wo ...

  8. hibernate复习第(二)天

    今日要点: 关联映射 多对一(Employee - Department) 一对多(Department - Employee) 一对一(Person - IdCard) 多对多(teachet - ...

  9. struts2--Basic(一)

    Struts是流行和成熟的基于MVC设计模式的WEB应用程序框架. 帮助我们减少在运用MVC设计模式来开发Web应用的时间. 1.下载添加jar包 2. 准备配置文件 web.xml <filt ...

  10. JQuery基本知识、选择器、事件、DOM操作、动画--2017年2月10日

    $(对象)可以将JS对象转换为JQuery对象  .get(0)可以将JQuery对象转换为JS对象 并无太大区别,灵活点出即可