c php中间件,thinkphp5.1 中间件的理解和整理

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

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

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

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

thinkphp5.1 中的中间件说明:

生成中间件:

php think make:middleware Check

复制代码

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

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对象,第二个参数是一个闭包

前置中间件/后置中间件

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

namespace app\http\middleware;

class auth{

public function handle($request, \Closure $next)

{

// 添加中间件执行代码

return $next($request);

}}

复制代码

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

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')

]

复制代码

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

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;

}

}

复制代码

namespace app\http\middleware;

class Auth

{

public function handle($request, \Closure $next)

{

$request->auth = 'auth';

return $next($request);

}

}

namespace app\http\middleware;

class Log

{

public function handle($request, \Closure $next)

{

$request->log = 'hello';

return $next($request);

}

}

/**

* 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值