search

Home  >  Q&A  >  body text

php - laravel5.4 prints Auth::check(); in the constructor of the class and returns false, and prints it in the method of the class and returns true

Help, help! laravel5.4 prints Auth::check(); in the constructor of the class and returns false, and prints it in the method of the class and returns true. Make sure you are logged in

Pictured:

returns false

怪我咯怪我咯2755 days ago1106

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-05-16 13:00:35

    Reason

    This problem has existed since Laravel 5.3 started grouping routes. The reason is that the middleware is not running when construct is run.

    Solution

    use Closure;
    
    class Controller extends BaseController
    {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
        protected $user;
    
        public function __construct(Request $request)
        {
            $this->middleware(function($request,Closure $next){
    
                $this->user = Auth::user();
    
                return $next($request);
    
            });
    
        }
    }

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:00:35

    function __construct(Request $request)

    {
        $this->middleware(function ($request, $next) {
            dd(Auth::check());
        });
    }

    reply
    0
  • Cancelreply