php laravel http,Laravel中HTTP内核的详细解析

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:55   2191   0

本篇文章给大家带来的内容是关于Laravel中HTTP内核的详细解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

Http Kernel

Http Kernel是Laravel中用来串联框架的各个核心组件来网络请求的,简单的说只要是通过public/index.php来启动框架的都会用到Http Kernel,而另外的类似通过artisan命令、计划任务、队列启动框架进行处理的都会用到Console Kernel, 今天我们先梳理一下Http Kernel做的事情。

内核绑定

既然Http Kernel是Laravel中用来串联框架的各个部分处理网络请求的,我们来看一下内核是怎么加载到Laravel中应用实例中来的,在public/index.php中我们就会看见首先就会通过bootstrap/app.php这个脚手架文件来初始化应用程序:

下面是 bootstrap/app.php 的代码,包含两个主要部分创建应用实例和绑定内核至 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;

HTTP 内核继承自 IlluminateFoundationHttpKernel类,在 HTTP 内核中 内它定义了中间件相关数组, 中间件提供了一种方便的机制来过滤进入应用的 HTTP 请求和加工流出应用的HTTP响应。<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel

{

/**

* The application's global HTTP middleware stack.

*

* These middleware are run during every request to your application.

*

* @var array

*/

protected $middleware = [

\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,

\App\Http\Middleware\TrimStrings::class,

\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

\App\Http\Middleware\TrustProxies::class,

];

/**

* The application's route middleware groups.

*

* @var array

*/

protected $middlewareGroups = [

'web' => [

\App\Http\Middleware\EncryptCookies::class,

\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

\Illuminate\Session\Middleware\StartSession::class,

// \Illuminate\Session\Middleware\AuthenticateSession::class,

\Illuminate\View\Middleware\ShareErrorsFromSession::class,

\App\Http\Middleware\VerifyCsrfToken::class,

\Illuminate\Routing\Middleware\SubstituteBindings::class,

],

'api' => [

'throttle:60,1',

'bindings',

],

];

/**

* The application's route middleware.

*

* These middleware may be assigned to groups or used inpidually.

*

* @var array

*/

protected $routeMiddleware = [

'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,

'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,

'can' => \Illuminate\Auth\Middleware\Authorize::class,

'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

];

}

在其父类 「IlluminateFoundationHttpKernel」 内部定义了属性名为 「bootstrappers」 的 引导程序 数组: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,

];

引导程序组中 包括完成环境检测、配置加载、异常处理、Facades 注册、服务提供者注册、启动服务这六个引导程序。

应用解析内核

在将应用初始化阶段将Http内核绑定至应用的服务容器后,紧接着在public/index.php中我们可以看到使用了服务容器的make方法将Http内核实例解析了出来:$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

在实例化内核时,将在 HTTP 内核中定义的中间件注册到了 路由器,注册完后就可以在实际处理 HTTP 诶

];*/

$this->app->bootstrapWith($this->bootstrappers());

}

}

发送响应

经过上面的几个阶段后我们最终拿到了要返回的响应,接下来就是发送响应了。//public/index.php

$response = $kernel->handle(

$request = Illuminate\Http\Request::capture()

);

// 发送响应

$response->send();

发送响应由 Illuminate\Http\Response的send()方法完成父类其定义在父类Symfony\Component\HttpFoundation\Response中。public function send()

{

$this->sendHeaders();// 发送响应头部信息

$this->sendContent();// 发送报文主题

if (function_exists('fastcgi_finish_request')) {

fastcgi_finish_request();

} elseif (!\in_array(PHP_SAPI, array('cli', 'phpdbg'), true)) {

static::closeOutputBuffers(0, true);

}

return $this;

}

关于Response对象的详细分析可以参看我们之前讲解Laravel Response对象的章节。

终止应用程序

响应发送后,HTTP内核会调用terminable中间件做一些后续的处理工作。比如,Laravel 内置的「session」中间件会在响应发送到浏览器之后将会话数据写入存储器中。// public/index.php

// 终止程序

$kernel->terminate($request, $response);//Illuminate\Foundation\Http\Kernel

public function terminate($request, $response)

{

$this->terminateMiddleware($request, $response);

$this->app->terminate();

}

// 终止中间件

protected function terminateMiddleware($request, $response)

{

$middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge(

$this->gatherRouteMiddleware($request),

$this->middleware

);

foreach ($middlewares as $middleware) {

if (! is_string($middleware)) {

continue;

}

list($name, $parameters) = $this->parseMiddleware($middleware);

$instance = $this->app->make($name);

if (method_exists($instance, 'terminate')) {

$instance->terminate($request, $response);

}

}

}

Http内核的terminate方法会调用teminable中间件的terminate方法,调用完成后从HTTP请求进来到返回响应整个应用程序的生命周期就结束了。

总结

本节介绍的HTTP内核起到的主要是串联作用,其中设计到的初始化应用、引导应用、将HTTP请求抽象成Request对象、传递Request对象通过中间件到达处理程序生成响应以及响应发送给客户端。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP