1
After a lot of research and taking a look at documentation, I fully understand that and why can not do the "old fashioned", but wanted a solution/ workaround to do the following:
I have a base class, it is not called directly in the request, it is not the one that is declared in the routes:
class AdminPanelController extends Controller
{
protected $_authUser;
public function __construct() {
$this->middleware(function ($request, $next) {
$this->_authUser = Auth::guard('admin')->user();
dd($this->_authUser); // tudo bem
return $next($request);
});
}
...
protected function yoo() {
dd($this->_authUser); // null, mas precisava disto aqui
}
}
Needed the authenticated user to be available in the method yoo()
, and the controller called directly with the request:
Route::get('users', 'UsersController@hey');
Userscontroller:
class UsersController extends AdminPanelController
{
protected params;
public function __construct(Request $request) {
parent::__construct();
$this->params = $this->yoo(); // auth user é null, mas precisava de chamar o metodo aqui
}
...
}
Note that if you call the method $this->yoo()
in a place other than the builder already works well, but I really need to call in the builder.
NOTE: I also tried $request->user()
with the result Authentication user provider [] is not defined.
, as this is a multiauthentication system would have to define a Guard: $request->guard('admin')->user()
that gave: Method guard does not exist
Because it is being assigned within the method
middleware
?– novic
@Virgilionovic, I honestly saw this solution by the Docs that are on the link. But it didn’t help me much
– Miguel
@Virgilionovic , the problem is not with the middleware that checks whether or not it is authenticated, this works perfectly
– Miguel
yes yes I understood, I did not also say that it is a problem only I questioned why to do it within the
middleware
Then you explained that you are in the documentation forLaravel 5.3.4
.– novic
@Virgilionovic, but as I say to you, it seems to me that with this or without it is the same. I will add an extra information to the question
– Miguel
It doesn’t really work, I assume from the premise that the
middleware
runs first and in this case no, I may be even mistaken, but, I tested the codes and do not have access toconstrutor
only in the methods when the same one already executed thatmiddleware
. So much so that if I put in the constructor of the class Adminpanelcontroller on the last lineecho $this->_authUser;
has nothing, because it has not yet been executed. The documentation in some cases leaves to be desired, unfortunately.– novic
But very obnoxious @Virgilionovic, for trying to help
– Miguel