先来张图大致理解下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. debian下蓝牙适配器的配置和使用

    本文打算将蓝牙适配器和手机蓝牙进行配对. 买了个支持蓝牙4.0协议的蓝牙适配器,将其插入到pc(debian 7.4)的usb口. 查看手机蓝牙信息: 选择手机中"设置"-> ...

  2. Spring Cloud之搭建动态Zuul网关路由转发

    传统方式将路由规则配置在配置文件中,如果路由规则发生了改变,需要重启服务器.这时候我们结合上节课内容整合SpringCloud Config分布式配置中心,实现动态路由规则. 将yml的内容粘贴到码云 ...

  3. 剑指offer之 O(1)时间删除链表结点

    问题描述:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. package Problem13; /* * 问题描述:给定单向链表的头指针和一个结点指针,定义一个函数在O(1 ...

  4. mysql学习笔记1

  5. Hadoop- MR的shuffle过程

    step1 input InputFormat读取数据,将数据转换成<key ,value>对,设置FileInputFormat,默认是文本格式(TextInputFormat) ste ...

  6. 程序以html形式发送邮件注意问题

    1.样式要写在内部,不要单独定义样式 2.div 使用背景图片在有些浏览器中出不来这时候需要用: <table style="width: 640px" cellpaddin ...

  7. curl常用命令行总结

    curl 有时HTTP服务接口写完,需要验证下接口功能,这个使用用curl最合适了 curl 全称 command line url viewer curl www.taobao.com curl w ...

  8. poj-1442 Black Box(Treap)

    题目链接: Black Box 题意: 给一个序列,m个询问,每个询问是求前x个数中的第i小是多少; 思路: Treap的入门题目;Treap能实现STL的set实现不了的功能,如名次树(rank t ...

  9. python实现无序列表:链表

    介绍链表前我们先了解下什么是列表. 在对基本数据结构的讨论中,我们使用 Python 列表来实现所呈现的抽象数据类型.列表是一个强大但简单的收集机制,为程序员提供了各种各样的操作.然而,不是所有的编程 ...

  10. poj2420 A Star not a Tree? 模拟退火

    题目大意: 给定n个点,求一个点,使其到这n个点的距离最小.(\(n \leq 100\)) 题解 模拟退火上 #include <cmath> #include <cstdio&g ...