thinkphp5.1 中间件是什么,有什么用

中间件是什么?有什么作用?

中间件主要用于拦截或过滤应用的HTTP请求,并进行必要的业务处理。---tp5.1手册

也就是说,降低了系统的耦合;【在http请求阶段,执行中间件的入口执行方法(handle)--tp5.1】----减少了系统的一些if/else判断,因此降低了系统的耦合

中间件可以实现什么功能,例如权限验证,访问记录,重定向等等。-----这些业务的存在降低了耦合

Thinkphp中间件有什么用?

消息队列、远程方法调用RPC框架、ODBC、ORM持久化框架、缓存、资源定位、中间件定义的边界并不是很清晰,介于应用逻辑和操作系统(网络、存储系统)之间抽象层都可以算作中间件。


thinkphp5.1 中的中间件说明:
生成中间件:

php think make:middleware Check

 这个指令会 application/http/middleware目录下面生成一个Check中间件

 1 <?php
 2  
 3 namespace app\http\middleware;
 4  
 5 class Check{
 6      //第三个参数,可以通过路由赋值传递
 7     public function handle($request, \Closure $next, $name)
 8     {
 9         //下面这一句是 给控制器 传值
10         $request->hello = 'ThinkPHP';
11          
12         if ($name == 'think') {
13             return redirect('index/think');
14         }
15  
16         return $next($request);
17     }
18      
19     }

 中间件的入口执行方法必须是handle方法,而且第一个参数是Request对象,第二个参数是一个闭包

前置中间件/后置中间件

前置中间件:在请求阶段实现,如:判断登录状态,访问权限等等

<?php
 
namespace app\http\middleware;
class auth{
    public function handle($request, \Closure $next)
    {
        // 添加中间件执行代码
 
        return $next($request);
    }}

后置中间件:请求完成之后实现,如:写日志,请求分析等等

<?php
 
namespace app\http\middleware;
class Log{
    public function handle($request, \Closure $next)
    {
        $response = $next($request);
 
        // 添加中间件执行代码
 
        return $response;
    }}

tp5.1中的配置文件:middleware.php【可以预先注册中间件,增加别名标识】,如果没有指定命名空间则默认使用app\http\middleware

return[
 
    'check' => app\http\middleware\Check:class,
    'auth' => app\http\middleware\Auth:class,
    'log' => app\http\middleware\Log:class
 
]

中间件的使用:【说明当一个方法里面有多个中间件【前置中间件】时,执行顺序按照 设置中间件使用的配置 的顺序执行,后置中间件的执行一定是在请求完成之后,才执行的,所以肯定是在最后才被执行】

一、在路由定义配置中设置,如:

return [
 
//下面路由注册的中间件,给中间件auth传递了"ahai",给中间件check传递了"token"参数,不写,则不传递参数
Route::rule('hello/:name','hello')->middleware(['auth:ahai','check:token','log']),
Route::rule('index/:name','think')->middleware('auth')
]

二、在控制器中设置,如:

 1 <?php
 2 namespace app\index\controller;
 3 use think\Controller;
 4 class Index extends Controller{
 5  
 6 //这里配置了中间件,同时限制了中间件的生效操作,
 7 //比如下面的auth中间件,使用了except,表示除了hello方法外,这个控制器的其他的方法都会执行中间件,
 8 //check中间件,使用了only表示只有这个控制器的hello方法执行这个中间件
 9 //log中间件,没有使用任何限定参数,表示这个控制器的所有方法都会执行log这个中间件
10     protected $middleware = [ 
11         'auth'     => ['except'   => ['hello'] ],
12         'check' => ['only'    => ['hello'] ],
13         'log'
14     ];
15  
16     public function index()
17     {
18         echo request()->auth;
19         echo request()->check;
20         echo request()->log;
21     }
22     public function login()
23     {
24         echo request()->auth;
25         echo request()->check;
26         echo $this->request->log;
27     }
28     public function hello()
29     {
30         echo $this->request->log;
31         echo request()->auth;
32         echo request()->check;
33     }
34      
35     }

 

 1 <?php
 2 namespace app\http\middleware;
 3 class Auth
 4 {
 5     public function handle($request, \Closure $next)
 6     {
 7         $request->auth = 'auth';
 8         return $next($request);
 9     }
10 }
11 <?php
12 namespace app\http\middleware;
13 class Log
14 {
15     public function handle($request, \Closure $next)
16     {
17          $request->log = 'hello';
18          return $next($request);
19     }
20 }
21 <?php
22 /**
23  * Created by ahai
24  * Time: 2018/9/27 0027 上午 10:18
25  * Email: <764882431@qq.com>
26  */
27 namespace app\http\middleware;
28 Class Check
29 {
30     public function handle($request, \Closure $next)
31     {
32         $request->check = 'check';
33         return $next($request);
34     }
35 }

 

 转自  http://www.thinkphp.cn/topic/59357.html

posted @ 2018-12-04 15:12  顺瓜摸藤  阅读(5978)  评论(0编辑  收藏  举报