3 weeks ago

Laravel 7, Get the Authenticate User before the controller class is initialized

Posted 3 weeks ago by mcode

Hi All,

From the old 5.2 LV to LV 7, I came across a issue regarding the Auth user is not set before the construct of a class (Controller)

In the old framework the Controllers extends a baseController where in the __constructor the Auth user is checked and many view shared information is cached and set...

IN the new LV 7 I cant gt that to work.. so It here a other way like in the old days the before filter method?

I dont want to call in every 100+ controller methods a custom function with the same logic like in the old __constructor.

  • Before initialized check if the Auth User check(), If so then get the user information (many data)
  • Before initialized set all shared view vars
  • etc

Can someone help?




Well I ended up doing this in the __construct:

$this->middleware(function ($request, $next) { $this->_oldConstructLogic (); return $next($request); });

And have a central method (baseController)

public function _oldConstructLogic() { // logic like \Auth::user() stuf and config settings etc... }

Please sign in or create an account to participate in this conversation.