代码:
<?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