zoukankan      html  css  js  c++  java
  • Laravel 入口文件解读及生命周期

    这里只贴index.php的代码, 深入了解的请访问   
    https://laravel-china.org/articles/10421/depth-mining-of-laravel-life-cycle   

    <?php
    
    /**
     * Laravel - A PHP Framework For Web Artisans
     * 一个为艺术而生的PHP框架
     * 生命周期一共分为三个阶段
     *      1、 加载依赖
     *      2、 实例化
     *      3、 接收请求并响应
     * @package  Laravel
     * @author   Taylor Otwell <taylor@laravel.com>
     */
    
    /*
    |--------------------------------------------------------------------------
    | Register The Auto Loader     注册自动加载类
    |--------------------------------------------------------------------------
    |
    | Composer provides a convenient, automatically generated class loader for
    | our application. We just need to utilize it! We'll simply require it
    | into the script here so that we don't have to worry about manual
    | loading any of our classes later on. It feels nice to relax.
    |
    */
    
    /**
     * 生命周期第一阶段 加载依赖
     * 在__DIR__.'/../bootstrap/autoload.php'中,
     * 先设置程序开始时间戳,
     * 再使用composer/autoload_real.php::getLoader()获取类名与类文件的映射然后注册组件类(根据PHP版本不同以及其他条件,分为静态注册和非静态注册)
     * 如果存在编译的缓存文件,则引入
     */
    require __DIR__.'/../bootstrap/autoload.php';
    
    /*
    |--------------------------------------------------------------------------
    | Turn On The Lights
    |--------------------------------------------------------------------------
    |
    | We need to illuminate PHP development, so let us turn on the lights.
    | This bootstraps the framework and gets it ready for use, then it
    | will load up this application so that we can run it and send
    | the responses back to the browser and delight our users.
    |
    */
    
    /**
     *  生命周期第二阶段,创建Laravel应用实例
     *  创建 APP 容器、注册应用路径、注册基础服务提供者(绑定内核),并在绑定内核时 配置中间件和引导程序等参数
     */
    $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(IlluminateContractsHttpKernel::class);
    //使用容器$app的make()方法实例化Http内核,
    // 在vendor/Laravel/framework/src/Illuminate/Foundation/Http/Kernel.php文件中
    // 构造函数内将在 HTTP 内核定义的 中间件组 注册到 路由器, 之后就可以调用 这些中间件 处理请求
    // 构造函数接收 APP 容器 和 路由器 两个参数
    
    
    $response = $kernel->handle(
        $request = IlluminateHttpRequest::capture()
        //请求实例 IlluminateHttpRequest 的 capture() 方法内部通过 Symfony 实例创建一个 Laravel 请求实例。
        //这样我们就可以获取到用户请求报文的相关信息了,以及全局变量等信息。
    
        //handle() 方法接收一个 HTTP 请求,
        //首先,将 $request 实例注册到 APP 容器 供后续使用;
        //之后,清除之前 $request 实例缓存;
        //然后,通过$this->bootstrap();启动「引导程序」;即我们第二阶段创建容器时 配置的引导程序 bootstrappers
        //然后,发送请求至路由,通过路由实例,查找routes/web.php中路由,最终匹配到控制器或匿名函数。
        //随即进入到请求处理的流程
        //并最终生成一个 HTTP 响应并返回到这里。
    );
    
    /**
     *  经过一系列漫长的处理,终于来到最后 -> 发送响应
     */
    $response->send();
    //发送响应由 IlluminateHttpResponse 父类 SymfonyComponentHttpFoundationResponse 中的 send() 方法完成。
    
    $kernel->terminate($request, $response);
    //程序终止,完成终止中间件的调用
  • 相关阅读:
    The 2019 ICPC Asia Shanghai Regional Contest H Tree Partition k、Color Graph
    回溯法、子集树、排列树、满m叉树
    顺时针打印矩阵
    单调递增的数字
    nodejs
    nodejs + express + mangodb 项目搭建
    nodejs + express 项目初始化
    星星评分功能(带小数点的那种,5颗星,10分制)
    easyui 增加删除toolbar 显示异常问题
    sql 外键级联,触发器防删
  • 原文地址:https://www.cnblogs.com/lz0925/p/10204910.html
Copyright © 2011-2022 走看看