lumen中间件 Middleware

霸气de小男生 提交于 2020-04-17 21:17:16

app/http 下新建 TestMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $params = $request->all();
        if(empty($params['id'])){
            $response = ['code'=>400,'msg'=>'参数错误','data'=>[]];
            return response()->json($response);
        }
        return $next($request);
    }
}

bootstrap/app.php里面启用

$app->routeMiddleware([
    //'auth' => App\Http\Middleware\Authenticate::class,
    'test'=>App\Http\Middleware\TestMiddleware::class
]);

  


路由启用中间件验证
$router->group(
    [
        'middleware'=>'test',
        'namespace' => 'Admin',
        "prefix"=>'admin'
    ]
    , function($router) {
    $router->get('user/info', "UserController@info");
});

  

  

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!