search

Home  >  Q&A  >  body text

php - Laravel 5.4 How to use shared variables as "global variables" in Controllers?

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_leeringa_lee2759 days ago1514

reply all(3)I'll reply

  • ringa_lee

    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

    reply
    0
  • 仅有的幸福

    仅有的幸福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?

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-05 11:11:17

    Middleware is your best choice laravel middleware documentation

    reply
    0
  • Cancelreply