laravel - Can't get session in controller constructor

后端 未结 8 1769
感动是毒
感动是毒 2020-12-05 19:02

In new laravel I can\'t get session in constructor. Why?

public function __construct()
{
    dd(Session::all()); //this is empty array
}

a

8条回答
  •  不知归路
    2020-12-05 19:37

    As of other answers no out of the box solution for it. But you still can access it using Middleware in constructor.

    So here is another hack

    public function __construct(){
        //No session access from constructor work arround
        $this->middleware(function ($request, $next){
            $user_id = session('user_id');
            return $next($request);
        });
    
    }
    

提交回复
热议问题