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");
});
来源:oschina
链接:https://my.oschina.net/u/4407852/blog/3270304