app/http 下新建 TestMiddleware.php
<?php namespace AppHttpMiddleware; use Closure; class TestMiddleware { /** * Handle an incoming request. * * @param IlluminateHttpRequest $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' => AppHttpMiddlewareAuthenticate::class, 'test'=>AppHttpMiddlewareTestMiddleware::class ]);
路由启用中间件验证
$router->group( [ 'middleware'=>'test', 'namespace' => 'Admin', "prefix"=>'admin' ] , function($router) { $router->get('user/info', "UserController@info"); });