将参数从Middleware传递到Controller

时间:2015-11-05 12:50:14

标签: php laravel laravel-5 laravel-5.1

我有一个中间件进行查询,我想将结果传回给我的控制器。可能吗?我正在使用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);
    }

控制器:

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);
}

我已将它注入到请求的属性中,但我无法从我的函数中获取它。如果我在我的index()函数中dd($ request-> attributes-> get('acl')),则数据可用。

1 个答案:

答案 0 :(得分:0)

您可以将变量存储在会话中:

Session::flash('result', $result);

然后您可以在控制器中检索它,如下所示:

Session::get('result');