thinkphp5.1---中间件的使用

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

定义中间件:可以通过命令行指令快速生成中间件

php think make:middleware Check

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

 

<?php
namespace app\http\middleware;
class Check
{
    public function handle($request, \Closure $next)
    {
    }
}

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

1、前置中间件

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

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

        return $next($request);
    }
}

2、后置中间件

后置中间件:主要是在请求之后实现,例如:写日志、请求分析等等。

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

注册使用中间件:

在tp5.1的配置文件 config/middleware.php中 【可以预先注册中间件,增加别名标识】  可用于增加别名

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

但是这个注册使用中间,如果定义了中间件的命名空间即可省略注册:

return [
    // 默认中间件命名空间
    'default_namespace' => 'app\\http\\middleware\\',
];

使用中间件:

Route::rule('hello/:name','hello')->middleware('Auth');

或者使用完整的路径:

Route::rule('hello/:name','hello')->middleware(app\http\middleware\Auth::class);

支持使用多个中间件:

Route::rule('hello/:name','hello')->middleware(['Auth', 'Check']);

我的尝试:

第一:在路由上使用

在middleware目录新建 Auth.php

<?php
namespace app\http\middleware;

class Auth{
  /**
   * 中间件拦截
   */
  public function handle($request, \Closure $next)
  {
      // 添加中间件执行代码
      if(2 > 1){
          return error('中间件拦截');
      }
      return $next($request);
  }
}

在路由上使用:

Route::get('addons/abc','addons/index/index')->middleware('Auth');

第二:在控制器中使用

在控制器中使用,配置的路由规则为:

Route::get('addons/abc','addons/index/index');

然后在控制器:定义 protected $middleware

代码示例:

<?php
namespace app\addons\controller;
use think\Controller;

class Index extends Controller{
    protected $middleware = [
        'Auth'
    ];
    public function index(){
        echo "this is a addons index controller index function";
    }
    public function login(){
        echo "abcd";
    }
    public function hello(){
        echo "abcd";
    }
}

这样使用中间件,意味着在这个控制器下的所有方法都需要Auth中间件的验证,解决方法:

处理情况:控制器中的某几个方法,不需要中间件来验证。

例如:

auth中间件,使用了except,表示出了hello方法外,这个控制器其他的方法都会执行这个中间件
check中间件,使用了only表示只有这个控制器的login方法执行这个中间件
log中间件,没有使用任何限定参数,表示这个控制器里面所有的方法都会执行log这个中间件

<?php
namespace app\addons\controller;
use think\Controller;

class Index extends Controller{
    // auth中间件,使用了except,表示出了hello方法外,这个控制器其他的方法都会执行这个中间件
    // check中间件,使用了only表示只有这个控制器的login方法执行这个中间件
    // log中间件,没有使用任何限定参数,表示这个控制器里面所有的方法都会执行log这个中间件
    protected $middleware = [
        'auth' => ['except' => ['hello']],
        'check' => ['only' => 'login'],
        'log'
    ];
    public function index(){
        echo "this is a addons index controller index function";
    }
    public function login(){
        echo "abcd";
    }
    public function hello(){
        echo "abcd";
    }
}

这就是中间件的一些使用方法。

posted @ 2021-02-01 17:59  帅到要去报警  阅读(1139)  评论(0编辑  收藏  举报