类别:ThinkPHP / 日期:2019-11-27 / 浏览:189 / 评论:0

从5.1.6+版本最先,正式引入中心件的支撑。

中心件重要用于阻拦或过滤运用的HTTP要求,并举行必要的营业处置惩罚。

定义中心件

能够经由过程命令行指令疾速生成中心件

php think make:middleware Check

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

<?php
namespace app\http\middleware;
class Check
{
    public function handle($request, \Closure $next)
    {
        if ($request->param('name') == 'think') {
            return redirect('index/think');
        }
        return $next($request);
    }
}

中心件的进口实行要领必需是handle要领,而且第一个参数是Request对象,第二个参数是一个闭包。

中心件handle要领的返回值必需是一个Response对象。

在这个中心件中我们推断当前要求的name参数即是think的时刻举行重定向处置惩罚。不然,要求将进一步通报到运用中。要让要求继续通报到运用程序中,只需运用 $request 作为参数去挪用回调函数 $next 。

在某些需求下,能够运用第三个参数传入分外的参数。

<?php
namespace app\http\middleware;
class Check
{
    public function handle($request, \Closure $next, $name)
    {
        if ($name == 'think') {
            return redirect('index/think');
        }
        return $next($request);
    }
}

前置/后置中心件

中心件是在要求细致的操纵之前照样以后实行,完全取决于中心件的定义自身。

下面是一个前置行动的中心件

<?php
namespace app\http\middleware;
class Before
{
    public function handle($request, \Closure $next)
    {
        // 增添中心件实行代码
        return $next($request);
    }
}

下面是一个后置行动的中心件

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

来个比较现实的例子,我们须要推断当前浏览器环境是在微信或支付宝

namespace app\http\middleware;
/**
 * 接见环境搜检,是不是是微信或支付宝等
 */
class InAppCheck
{
    public function handle($request, \Closure $next)
    {
        if (preg_match('~micromessenger~i', $request->header('user-agent'))) {
            $request->InApp = 'WeChat';
        } else if (preg_match('~alipay~i', $request->header('user-agent'))) {
            $request->InApp = 'Alipay';
        }
        return $next($request);
    }
}

然后在你的挪动版的module里增添一个middleware.php文件

比方:/path/application/mobile/middleware.php

return [
    app\http\middleware\InAppCheck::class,
];

然后在你的controller中能够经由过程$this->request->InApp猎取相干的值

注册中心件

路由中心件

最经常使用的中心件注册体式格局是注册路由中心件

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

V5.1.7+版本,你能够直接在运用设置目次下的middleware.php中先预定义中心件(实在就是增添别号标识),比方:

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

然后直接在路由中运用中心件别号注册

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

V5.1.8+版本最先,能够支撑运用别号定义一组中心件,比方:

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

然后,直接运用下面的体式格局注册中心件

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

支撑对路由分组注册中心件

Route::group('hello', function(){
Route::rule('hello/:name','hello');
})->middleware('Auth');

V5.1.8+版本最先支撑对某个域名注册中心件

Route::domain('admin', function(){
// 注册域名下的路由划定规矩
})->middleware('Auth');

假如须要传入分外参数给中心件,能够运用

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

假如运用的是常量体式格局定义,能够在第二个参数传入中心件参数。

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

假如须要定义多个中心件,运用数组体式格局

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

能够一致传入同一个分外参数

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

或许零丁指定中心件参数。

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

运用闭包定义中心件

你不一定要运用中心件类,在某些简朴的场所你能够运用闭包定义中心件,但闭包函数必需返回Response对象实例。

Route::group('hello', function(){
Route::rule('hello/:name','hello');
})->middleware(function($request,\Closure $next){
    if ($request->param('name') == 'think') {
        return redirect('index/think');
    }
    
return $next($request);
});

全局中心件

你能够在运用目次下面定义middleware.php文件,运用下面的体式格局:

<?php
return [
\app\http\middleware\Auth::class,
    'Check',
    'Hello',
];

中心件的注册应当运用完全的类名,假如没有指定定名空间则运用app\http\middleware作为定名空间。

全局中心件的实行递次就是定义递次。能够在定义全局中心件的时刻传入中心件参数,支撑两种体式格局传入。

<?php
return [
[\app\http\middleware\Auth::class, 'admin'],
    'Check',
    'Hello:thinkphp',
];

上面的定义示意 给Auth中心件传入admin参数,给Hello中心件传入thinkphp参数。

模块中心件

V5.1.8+版本最先,支撑模块中心件定义,你能够直接在模块目次下面增添middleware.php文件,定义体式格局和运用中心件定义一样,只是只会在该模块下面见效。

控制器中心件

V5.1.17+版本最先,支撑为控制器定义中心件。起首你的控制器须要继续体系的think\Controller类,然后在控制器中定义middleware属性,比方:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    protected $middleware = ['Auth'];
    public function index()
    {
        return 'index';
    }
    public function hello()
    {
        return 'hello';
    }
}

当实行index控制器的时刻就会挪用Auth中心件,一样支撑运用完全的定名空间定义。

假如须要设置控制器中心的见效操纵,能够以下定义:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    protected $middleware = [ 
    'Auth' => ['except' => ['hello'] ],
        'Hello' => ['only' => ['hello'] ],
    ];
    public function index()
    {
        return 'index';
    }
    public function hello()
    {
        return 'hello';
    }
}

中心件向控制器传参

能够经由过程给要求对象赋值的体式格局传参给控制器(或许别的处所),比方

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

注重,通报的变量称号不要和param变量有争执。

然后在控制器的要领内里能够直接运用

public function index(Request $request)
{
return $request->hello; // ThinkPHP
}

本文来自ThinkPHP框架技术文章栏目:http://www.ki4.cn/phpkj/thinkphp/

以上就是thinkphp中心件是什么意义的细致内容,更多请关注ki4网别的相干文章!

打赏

感谢您的赞助~

打开支付宝扫一扫,即可进行扫码打赏哦~

版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。

 可能感兴趣的文章