laravel 生命周期

本文详细介绍了laravel应用的生命周期,从创建应用实例、内核绑定到处理HTTP请求,再到发送响应和终止程序。重点讲解了内核如何处理请求,包括启动引导程序、中间件和路由分发,以及响应的生成与发送过程。
摘要由CSDN通过智能技术生成

代码:

<?php
//laravel 启动时间
define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
    加载项目依赖
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
创建laravel应用实例
|
*/

$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(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
//结束请求,进行回调
$response->send();
//终止程序
$kernel->terminate($request, $response);

2.2创建laravel应用实例

创建应用实例(服务容器),由 bootstrap/app.php 文件里的引导程序完成,创建服务容器的过程就是应用初始化的过程,项目初始化过程包括(注册项目基础服务,注册项目服务提供者别名,注册目录路径等在内的一系列注册工作

<?php

//1:创建应用实例

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

//2:完成内核绑定 ,待后续使用时实例

$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;

2.2.1 创建应用实例

创建应用实例 就是实例化  Illuminate\Foundation\Application 这个类 这个实例我们可称作app容器,在完成实例化时 执行构造函数 注册应用的基础路径并将路径绑定到容器中,注册基础服务提供者到容器中,注册核心容器别名到容器中

<?php

//文件路径  Illuminate\Foundation\Application

public function __construct($basePath = null)
    {
        if ($basePath) {
            $this->setBasePath($basePath);
        }
        
        $this->registerBaseBindings();

        $this->registerBaseServiceProviders();

        $this->registerCoreContainerAliases();
    }

2.2.2 内核绑定

laravel 根据http请求的运行环境的不同 将请求发送至相应的内核:http内核 或者console内核. 二者的作用都是接受请求,然后返回一个响应

以http内核为例,http内核(app/Http/Kernel.php ) 继承Illuminate\Foundation\Http\Kernel类

http内核 定义了中间件,而Illuminate\Foundation\Http\Kernel类 定义了 $bootstrappers 引导程序数组

中间件:提供了一种方便的机制来过滤进入应用的http请求

引导程序:包括完成环境监测,配置加载,服务提供者注册,facades注册,异常处理器注册,启动服务这个六个领导程序

2.2.3注册异常处理

项目的异常处理有App\Exceptions\Handler::class 完成

2.3接受请求并返还响应

完成创建app容器后 就进入接受请求并响应的过程

接收请求并响应代码如下

|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值