[英]Passing parameter from Middleware to Controller
I have a middleware which does a query and I would like to pass the result back to my controller. 我有一个执行查询的中间件,我想将结果传递回我的控制器。 Is it possible?
可能吗? I'm using Laravel 5.
我正在使用Laravel 5。
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
if ($request->ajax()) {
return view('errors.401');
} else {
return redirect()->guest('auth/login');
}
}
$cur_route = Request::segment(1);
$result = DB::table('t_user_acl_masters')->join('t_function_details', 't_function_details.functiondtl_id', '=' ,'t_user_acl_masters.functiondtl_id')
->where('t_function_details.routename', '=', $cur_route)
->where('t_user_acl_masters.user_id', '=', $request->user()->user_id)
->select('t_user_acl_masters.viewaccess','t_user_acl_masters.addaccess','t_user_acl_masters.editaccess','t_user_acl_masters.deleteaccess')
->first();
if(!empty($result)) {
if ($result->viewaccess != 'y') {
return view('errors.401');
}
}
return $next($request, $result);
}
Controller: 控制器:
public function __construct(Request $request)
{
parent::__construct();
$this->middleware('auth');
$this->user = Auth::user();
$this->acl = $request->attributes->get('acl');
}
public function index(Request $request){
dd($this->acl);
}
I have injected it in to the attributes of request, but I can't get it from my function. 我已经将其注入到request的属性中,但是无法从函数中获取它。 If I dd($request->attributes->get('acl')) in my index() function, data is available.
如果我在index()函数中dd($ request-> attributes-> get('acl')),则数据可用。
You can store the variable in the session: 您可以将变量存储在会话中:
Session::flash('result', $result);
Then you can retrieve it in your controller like so: 然后,您可以像这样在控制器中检索它:
Session::get('result');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.