Since Laravel 5.3, you cannot use Auth::check()
and Auth::user()
in the Controller's __construct()
method. Function (because Session has not started to work)
(You can refer to the official documentation for instructions in the Controller section, But this method is not applicable to this scenario)
Too lazy to write in every function
if (Auth::check()) {
$user = Auth::user()
}
The only lazy way at present is the Controller global variable. Google found that someone used the App::before()
method to do it before, but this method seems to have been canceled in 5.4?
Does anyone know how to implement this in Laravel 5.4?
Everything implemented using Config is broken up. It is obviously not suitable here.
ringa_lee2017-06-05 11:11:17
Thanks to @安正超 for the solution provided in Laravel China: How to get the logged-in user in the Laravel 5.3+ controller
仅有的幸福2017-06-05 11:11:17
First of all, user verification can be completely controlled through middleware, and it is not appropriate to put it in the constructor of the controller.
In addition, Container
is a good thing. It can not only save instances, but also save some global variables?