thinkphp5.1 中间件的理解和整理

浏览:28162 发布日期:2018/09/27 分类:ThinkPHP5专区 关键字: thinkphp5.1 中间件 middleware
中间件是什么?有什么作用?

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

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

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

thinkphp5.1 中的中间件说明:
生成中间件:php think make:middleware Check这个指令会 application/http/middleware目录下面生成一个Check中间件。<?php
 
namespace app\http\middleware;
 
class Check{
     //第三个参数,可以通过路由赋值传递
    public function handle($request, \Closure $next, $name)
    {
        //下面这一句是 给控制器 传值
        $request->hello = 'ThinkPHP';
         
        if ($name == 'think') {
            return redirect('index/think');
        }
 
        return $next($request);
    }
     
    }
中间件的入口执行方法必须是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\middlewarereturn[
 
    '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')
]
二、在控制器中设置,如:<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller{
 
//这里配置了中间件,同时限制了中间件的生效操作,
//比如下面的auth中间件,使用了except,表示除了hello方法外,这个控制器的其他的方法都会执行中间件,
//check中间件,使用了only表示只有这个控制器的hello方法执行这个中间件
//log中间件,没有使用任何限定参数,表示这个控制器的所有方法都会执行log这个中间件
    protected $middleware = [ 
        'auth'     => ['except'   => ['hello'] ],
        'check' => ['only'    => ['hello'] ],
        'log'
    ];
 
    public function index()
    {
        echo request()->auth;
        echo request()->check;
        echo request()->log;
    }
    public function login()
    {
        echo request()->auth;
        echo request()->check;
        echo $this->request->log;
    }

    public function hello()
    {
        echo $this->request->log;
        echo request()->auth;
        echo request()->check;
    }
     
    }
<?php

namespace app\http\middleware;

class Auth
{
    public function handle($request, \Closure $next)
    {
        $request->auth = 'auth';
        return $next($request);
    }
}
<?php
namespace app\http\middleware;

class Log
{
    public function handle($request, \Closure $next)
    {
         $request->log = 'hello';
         return $next($request);
    }
}
<?php
/**
 * Created by ahai
 * Time: 2018/9/27 0027 上午 10:18
 * Email: <764882431@qq.com>
 */
namespace app\http\middleware;

Class Check
{
    public function handle($request, \Closure $next)
    {
        $request->check = 'check';
        return $next($request);
    }
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>