先来张图大致理解下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. day3--集合、文件操作、字符编码与转换、函数(递归,lambda,filter,map)、字典排序

    list1 = set([1, 2, 3, 4, 5, 6, 5, 5, 5])list2 = set([11, 2, 36, 'a', 5, 6, 5, 5, 5])list3 = set([1, ...

  2. 登陆获取shell时的配置文件加载过程

    最近遇到一台ubuntu服务器登陆时默认语言环境变量变成posix问题, 导致中文显示乱码,影响程序的正常运行 # locale LANG= LANGUAGE= LC_CTYPE="POSI ...

  3. ubuntu下搭建Scrapy框架简单办法

    1. 先执行以下命令 sudo apt-get install python-lxml sudo apt-get install libxslt1-dev sudo apt-get install p ...

  4. 大话设计模式--解释器模式 interpreter -- C++实现实例

    1. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...

  5. CDH- 测试mr

    cdh的mr样例算法的jar包在 [zc.lee@ip---- hadoop-0.20-mapreduce]$ pwd /opt/cloudera/parcels/CDH--.cdh5./lib/ha ...

  6. 仿新浪游戏频道js多栏目全屏下拉菜单导航条

    仿新浪游戏频道js多栏目全屏下拉菜单导航条,新浪,游戏频道,js组件,多栏目,全屏下拉,下拉菜单,导航条.代码下载地址:http://www.huiyi8.com/sc/26765.html更多请访问 ...

  7. AOP学习(2)

    <property name="interceptorNames"> <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们 也可 ...

  8. js 禁止用户使用Ctrl+鼠标滚轮缩放网页

    为什么会有人会使用ctrl+鼠标滚轮缩放网页?坚决禁止! <html> <head> <title>测试</title> <script lang ...

  9. 分享知识-快乐自己:解决 Maven 无法下载 fastdfs-client-java 依赖。

    因为fastdfs-client-java-1.27-SNAPSHOT.jar这个依赖包在maven中央仓库是没有的. 需要自己编译源码成jar本地安装到maven 的本地仓库,安装完以后就能正常引用 ...

  10. adb命令(二)

    1.获取手机型号指令 adb shell cat /system/build.prop | findstr "ro.product.model" 2.获取手机处理器信息 adb s ...