码迷,mamicode.com
首页 > Web开发 > 详细

Laravel中间件原理

时间:2017-07-03 11:27:47      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

本文和大家分享的主要是Laravel中间件原理相关内容,一起来看看吧,希望对大家学习Laravel有所帮助。

Laravel 中间件提供了一种方便的机制来过滤进入应用的 HTTP 请求如 ValidatePostSize 用来验证 POST 请求体大小、 ThrottleRequests 用于限制请求频率等。

Laravel的中间件是怎样工作的呢?

启动流程

再说 Laravel 中间件前,我们先来理一理 laravel 的启动流程

首先,入口文件 index.php 加载了 autoload 和引导文件 bootstrap

require __DIR__.’/../bootstrap/autoload.php’;

$app = require_once __DIR__.’/../bootstrap/app.php’;

并在引导文件 bootstrap/app.php 中初始化了 Application 实例

$app = new Illuminate\\Foundation\\Application(

realpath(__DIR__.’/../’)

);

我们先跳过 如何初始化Application (后面会有简单介绍),再回到入口文件(index.php)中,通过从 Application 实例中获取 Http Kernel 对象来执行 handle 方法,换取 response

$kernel = $app->make(Illuminate\\Contracts\\Http\\Kernel::class);

$response = $kernel->handle(

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

);

$response->send();$kernel->terminate($request, $response);

换取响应后,把响应内容返回给 Client ,并执行后续操作(terminate,如关闭session等)。

实例化 Application 

Laravel的容器并不是我这次说的重点,这里简单介绍下

在初始化 Application (启动容器)时, Laravel 主要做了三件事情

1.注册基础绑定

2.注册基础服务提供者

3.注册容器核心别名

注册完成以后,我们就能直接从容器中获取需要的对象(如Illuminate\\Contracts\\Http\\Kernel),即使它是一个 Interface 

获取Illuminate\\Contracts\\Http\\Kernel类时,得到的真正实例是AppHttpKernel

Handle

从容器中获得 Http Kernel 对象后, Laravel 通过执行 kernel->handle 来换取 response对象。

//Illuminate\\Foundation\\Http\\Kernel.php

public function handle($request){

$request->enableHttpMethodParameterOverride();

$response = $this->sendRequestThroughRouter($request);

//......

}

enableHttpMethodParameterOverride 方法开启方法参数覆盖,即可以在 POST 请求中添加_method 参数来伪造 HTTP 方法(如post中添加_method=DELETE来构造 HTTP DELETE 请求)。

然后 Laravel 把请求对象request )通过管道流操作。

protected function sendRequestThroughRouter($request){

return (new Pipeline($this->app))

->send($request)

->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)

->then($this->dispatchToRouter());

}

/**

* Get the route dispatcher callback.

*

@return \\Closure

*/protected function dispatchToRouter(){

return function ($request) {

$this->app->instance(’request’, $request);

return $this->router->dispatch($request);

};

}

Pipeline laravel的管道操作类。在这个方法中,发送一个 $request 对象通过 middleware中间件数组,最后在执行 dispatchToRouter 方法。注意,这里的中间件只是 全局中间件 。即首先让Request通过全局中间件,然后在路由转发中($this->dispatchToRouter()),再通过 路由中间件  中间件group 

所以,到这里为止, Laravel 的请求交给了 Pipeline 管理,让我们来看看这个 Pipeline 究竟是怎样处理的。

//Illuminate\\Pipeline\\Pipeline.php

public function then(Closure $destination){

$pipeline = array_reduce(

array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)

);

return $pipeline($this->passable);

}

protected function prepareDestination(Closure $destination){

return function ($passable) use ($destination) {

return $destination($passable);

};

}

protected function carry(){

return function ($stack, $pipe) {

return function ($passable) use ($stack, $pipe) {

if ($pipe instanceof Closure) {

return $pipe($passable, $stack);

elseif (! is_object($pipe)) {

list($name, $parameters) = $this->parsePipeString($pipe);

$pipe = $this->getContainer()->make($name);

$parameters = array_merge([$passable, $stack], $parameters);

else {

$parameters = [$passable, $stack];

}

return $pipe->{$this->method}(...$parameters);

};

};

}

我们来看看最重要的 then 方法, 在这方法中 $destination 表示通过该管道最后要执行的 Closure(即上述的 dispatchToRouter 方法)  passable 表示被通过管道的对象 Request 

php 内置方法 array_reduce 把所有要通过的中间件$this->pipes )都通过 carry 方法($this->pipes不为空时)并<压缩>为一个 Closure 。最后在执行 prepareDestination 

array_reduce($pipes, callback($stack, $pipe), $destination), 当pipes为空时,直接执行destination,否则将所有 $pipes 压缩为一个 Closure ,最后在执行 destination 

列如我有两个中间件

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

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

将这两个中间件通过 array_reduce 方法时,返回的Closure:

Closure共有三个use, 前面两个为两个中间件,后面个位最后要执行的Closure(即上述的 dispatchToRouter 方法)。

//中间件handlepublic function handle($request, Closure $next){

}

在第一个通过的中间件(此处是 CheckForMaintenanceMode  handle 方法中, $next dump如下


在第二个通过的中间件(共两个,此处是 ValidatePostSize  handle 方法中, $next dump如下


由此可知,中间件在执行 $next($request) 时,表示该中间件已正常通过,并期待继续执行下一个中间件。直到所有中间件都执行完毕,最后在执行最后的 destination (即上述的 dispatchToRouter 方法)

以上是 Laravel 在通过全局中间件时的大致流程,通过中间件group和路由中间件也是一样的, 都是采用管道流操作

 

 

来源:SegmentFault

Laravel中间件原理

标签:

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
jiangjie190
加入时间:2016-02-19
  关注此人  发短消息
文章分类
jiangjie190”关注的人------(0
jiangjie190”的粉丝们------(0
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!