php - Laravel 5.4 How to use shared variables as "global variables" in Controllers?
ringa_lee
ringa_lee 2017-06-05 11:09:17
0
3
1470

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_lee
ringa_lee

ringa_lee

reply all(3)
左手右手慢动作

Thanks to @安正超 for the solution provided in Laravel China: How to get the logged-in user in the Laravel 5.3+ controller

仅有的幸福

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?

淡淡烟草味

Middleware is your best choice laravel middleware documentation

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template