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
怪我咯
怪我咯 2017-05-16 12:58:35
0
2
1131

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

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
小葫芦

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

        });

    }
}
迷茫

function __construct(Request $request)

{
    $this->middleware(function ($request, $next) {
        dd(Auth::check());
    });
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template