This article mainly introduced the Laravel framework user login Authentication implementation method, combined with the example form analysis the Laravel frame user login Authentication principle, the realization method and the related attention matters, the need friend can refer to the next
This paper describes the implementation method of Laravel framework user login authentication. Share to everyone for your reference, as follows:
Laravel detects if the user is logged in, the following code is available:
if (! Auth::guest ()) { return redirect::to ('/dashboard ');}
Auth::guestHow is that called?
Laravel uses the facade mode, the related façade class in the Laravel/framework/src/illuminate/support/facades folder definition, see the definition of the Auth class:
Class Auth extends Facade { /** * Get The registered name of the component. * * @return String * /protected static function Getfacadeaccessor () {return ' Auth ';}}
Laravel frame, facade mode uses reflection, the related method actually calls app[' auth ' in the method, when app[' auth ' is created,
AuthServiceProvider::registerMethod will register:
$this->app->bindshared (' auth ', function ($app) { //Once The authentication service has actually been requested By the developer //We'll set a variable in the application indicating such. This helps us //know so we need to set any queued cookie in the after event later. $app [' auth.loaded '] = true; return new AuthManager ($app);});
So why would it eventually be transferred to, look at the stack:
Illuminate\support\facades\auth::guest () illuminate\support\facades\facade::__callstaticilluminate\auth\ Authmanager->guest () illuminate\support\manager->__callpublic function __call ($method, $parameters) { return Call_user_func_array (Array ($this->driver (), $method), $parameters);}
Look at the code for driver:
Public Function driver ($driver = null) { $driver = $driver?: $this->getdefaultdriver (); If the given driver have not been created before, we'll create the instances //here and cache it so we can return It next time very quickly. If there is//already a driver created by this name, we'll just return that instance. if (! isset ($this->drivers[$driver])) { $this->drivers[$driver] = $this->createdriver ($driver); } return $this->drivers[$driver];}
The getdefaultdrive method is not called
/*** Get The default authentication driver name.** @return string*/public function Getdefaultdriver () { return $this- >app[' config ' [' auth.driver '];}
The final call is the driver configured in the configuration file, if the match is
' Driver ' = ' eloquent '
Then the call is
Public Function Createeloquentdriver () { $provider = $this->createeloquentprovider (); return new Guard ($provider, $this->app[' Session.store ');}
So Auth::guest the final call is the Guard::guest method
The logic here is to take the user information from the session, it is strange that only the user ID is stored in the session, and then take the ID to fetch the user information from the database.
Public Function User () {if ($this->loggedout) return; If we have already retrieved, the user for the current request, we can just//return it back immediately. We don't want to pull the user data every//request into the method because that would tremendously slow an app. if (! Is_null ($this->user)) {return $this->user; } $id = $this->session->get ($this->getname ()); First we'll try to load the user using the identifier in the session if//one exists. Otherwise We'll check for a "Remember Me" cookie in this//request, and if one exists, attempt to retrieve the user Using that. $user = null; if (! Is_null ($id)) {//provider is eloquentuserprovider $user = $this->provider->retrievebyid ($id); }//If the user is null and we decrypt a "recaller" cookie we can attempt to//pulling the user data on that cookie Which serves as a remember cookie on//the application. Once We have a user we CA return it to the caller. $recaller = $this->getrecaller (); if (Is_null ($user) &&! is_null ($recaller)) {$user = $this->getuserbyrecaller ($recaller); } return $this->user = $user;}