id?" />
C
C
coderisimo2016-09-08 15:24:58
Laravel
coderisimo, 2016-09-08 15:24:58

What happens "under the hood" when calling Laravel: Auth::user()->id?

I'm trying to figure it out with laravel. Very positive attitude. I stoically suppress the urge to write the entire project in ungodly codeigniter in two days.
Brains swell from facades, service providers and other x ... ..progressive stuff. There is a feeling that it is not given to me, but I want to try.
So Laravel: Auth::user()->id . Everyone advises: write like this and you will get the user id. But I would like to understand where it comes from. At first glance, the static user() method of the Auth class is called, which returns an object that has an id property. At second glance, Auth is a facade. Okay, not a showcase. Ok, we are trying to understand where the user () method and the id property come from.
Open vendor\laravel\framework\src\Illuminate\Support\Facades\Auth.php
protected static function getFacadeAccessor()
{
return 'auth';
}
go to vendor\laravel\framework\src\Illuminate\Auth find a service provider in it (again scary words!)
vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php
in it register() method
we see such a beast - $this->registerAuthenticator();
protected function registerAuthenticator()
{
$this->app->singleton('auth', function ($app) {
$app['auth.loaded'] = true;
return new AuthManager($app);
});
so AuthManager is the class we need. It's also , says the mapping between Laravel's facades and their underlying classes. Illuminate\Auth\AuthManager - auth
But as it turns out, the AuthManager class doesn't provide a user() .. method, nothing like that. There is no user() method there. So where does the notorious user() come from. What kind of object does it return to us? How much more to crawl through the code to understand what's what?
XXX….r.r..b.b!
Thanks for the comments.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
JhaoDa, 2016-09-08
@coderisimo

AuthManagermanages the drivers (guard), which provide the user(). Find the method in the manager __calland you will see that it proxies non-existent method calls to the driver.

A
Alexander Aksentiev, 2016-09-08
@Sanasol

https://laravel.com/api/5.3/Illuminate/Auth/Guard.html

E
Elena Stepanova, 2016-09-16
@Insolita

master xdebug https://laracasts.com/series/how-to-be-awesome-in-...
https://laravelista.com/posts/debugging-a-laravel-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question