lumen 中间件return $next($request); 传递参数到下一个controller怎么获取?

lumen
web.php中定义路由,post方法提交age参数,经过中间件auth_age:
$app->group(['middleware'=>'auth_age'],function()use($app){
$app->group(['namespace'=>'\App\Account\Http\Controllers'],function()use($app){
$app->post('/age',[
'as'=>'age',
'uses'=>'UserController@getAge'
]);
});
});
app.php中已注册auth_age:
$app->routeMiddleware([
'auth_age'=>App\Account\Http\Middleware\AgeMiddleware::class,
]);
中间件AgeMiddleware.php:
publicfunctionhandle($request,Closure$next)
{
//echo$request->input('age');exit;//post到这里能获取到
if($request->input('age')<18){
returnredirect('/');
}
return$next($request);//这里跳转到UserController@getAge
}
UserController.php
publicfunctiongetAge($age)//这里的$age参数无法获取,且报错
{
return"ageis".$age.".";
}
不加$age参数的话没有出错信息
publicfunctiongetAge(){
...
请教的问题:如何把post后的参数经过中间件,也能完整的传给后面的Controller?
一只名叫tom的猫
浏览 1037回答 2
2回答

慕侠2389804

自己回答:UserController.php..useIlluminate\Http\Request;...publicfunctiongetAge(Request$request){$age=$request->input('age');return"ageis".$age.".";}

江户川乱折腾

$input=$request->all();//或者$age=$request->input('age');因为经常写API,我个人的习惯是用all()$check_result=check_required_data($input,['login_type','login_value']);if($check_result['code']==404){returnresponse(['msg'=>$check_result['msg']],$check_result['code']);}.../***检测需要的变量**@paramarray$input_data*@paramarray$required_data**@returnarray*/publicfunctioncheck_required_data($input_data=[],$required_data=[]){foreach($required_dataas$value){if(idx($input_data,$value,false)===false){$dis=str_replace('_','',$value);//自定义变量习惯return['msg'=>'require'.$dis,'code'=>'404'];}}return['msg'=>'success','code'=>'200'];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript