Laravel 框架启动过程

1、入口文件

index.php

导入 composer 自动加载功能 ./bootstrap/autoload.php

2、启动文件

导入laravel容器 ./bootstrap/app.php

  1. 启动容器
    1. 注册容器自身(registerBaseBindings)
    2. 注册事件系统和路由系统(registerBaseServiceProviders)
    3. 注册laravel自有类别名(registerCoreContainerAliases)
  2. 注册Http处理器
  3. 注册Cli处理器
  4. 注册异常处理器

3、Http处理器

  • 启动Http处理器:Illuminate\Contracts\Http\Kernel->handle($request)

  • 进入Http请求处理工作:Illuminate\Contracts\Http\Kernel->sendRequestThroughRouter

    1. 重新在容器中绑定request
    2. 启动并配置HTTP相关的服务组件(Illuminate\Foundation\Bootstrap\ 中的所有组件)

       启动顺序:
       1. DetectEnvironment Dotenv 配置
       2. LoadConfiguration Config 配置 (/bootstrap/cache/config.php 或者 /config/*)
       3. ConfigureLogging 日志系统配置
       4. HandleExceptions php报错配置
       5. RegisterFacades 注册外观
       6. RegisterProviders 注册用户服务提供器
       7. BootProviders 启动所有提供器
      
    3. 路由管理器 Pipeline :

      穿过全局中间件 $middleware

      Router->dispatch(Request $request);

      Router->dispatchToRoute(Request $request);

      Router->runRouteWithinStack(Route $route, Request $request)

    4. 匹配到路由 Pipeline :

      穿过路由中间件 $routeMiddleware

      Route->run(Request $request)

      Route->runController(Request $request)

      Controller->method()

    5. 业务逻辑

      • 处理反射
      • 用户业务逻辑
    6. 处理Response
      • 字符串
      • 普通Response
      • 跳转Response
      • JsonResponse
      • ViewResponse

4、请求返回

  1. 应答对象发送到浏览器 $response->send();
  2. 运行可终止中间件(可终止中间件会是新的中间件实例,除非中间件被设置为单例模式)
  3. 应用终止后处理 Illuminate\Contracts\Http\Kernel->terminate($request, $response)

results matching ""

    No results matching ""