Laravel框架用户登陆身份验证实现方法详解
本文实例讲述了Laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下:
laravel中检测用户是否登录,有以下的代码:
if(!Auth::guest()) { returnRedirect::to('/dashboard'); }
那Auth::guest是如何调用的呢?
laravel用了Facade模式,相关门面类在laravel/framework/src/Illuminate/Support/Facades文件夹定义的,看下Auth类的定义:
classAuthextendsFacade{ /** *Gettheregisterednameofthecomponent. * *@returnstring */ protectedstaticfunctiongetFacadeAccessor(){return'auth';} }
laravel框架中,Facade模式使用反射,相关方法其实调用app['auth']中的方法,app['auth']是什么时候创建的呢,
AuthServiceProvider::register方法会注册:
$this->app->bindShared('auth',function($app) { //Oncetheauthenticationservicehasactuallybeenrequestedbythedeveloper //wewillsetavariableintheapplicationindicatingsuch.Thishelpsus //knowthatweneedtosetanyqueuedcookiesintheaftereventlater. $app['auth.loaded']=true; returnnewAuthManager($app); });
那为什么最终会调到哪里呢,看下堆栈:
Illuminate\Support\Facades\Auth::guest() Illuminate\Support\Facades\Facade::__callStatic Illuminate\Auth\AuthManager->guest() Illuminate\Support\Manager->__call publicfunction__call($method,$parameters) { returncall_user_func_array(array($this->driver(),$method),$parameters); }
看下driver的代码:
publicfunctiondriver($driver=null) { $driver=$driver?:$this->getDefaultDriver(); //Ifthegivendriverhasnotbeencreatedbefore,wewillcreatetheinstances //hereandcacheitsowecanreturnitnexttimeveryquickly.Ifthereis //alreadyadrivercreatedbythisname,we'lljustreturnthatinstance. if(!isset($this->drivers[$driver])) { $this->drivers[$driver]=$this->createDriver($driver); } return$this->drivers[$driver]; }
没有会调用getDefaultDrive方法
/** *Getthedefaultauthenticationdrivername. * *@returnstring */ publicfunctiongetDefaultDriver() { return$this->app['config']['auth.driver']; }
最终调用的是配置文件中配置的driver,如果配的是
'driver'=>'eloquent'
则调用的是
publicfunctioncreateEloquentDriver() { $provider=$this->createEloquentProvider(); returnnewGuard($provider,$this->app['session.store']); }
所以Auth::guest最终调用的是Guard::guest方法
这里的逻辑先从session中取用户信息,奇怪的是session里只保存的是用户ID,然后拿这个ID来从数据库中取用户信息
publicfunctionuser() { if($this->loggedOut)return; //Ifwehavealreadyretrievedtheuserforthecurrentrequestwecanjust //returnitbackimmediately.Wedonotwanttopulltheuserdataevery //requestintothemethodbecausethatwouldtremendouslyslowanapp. if(!is_null($this->user)) { return$this->user; } $id=$this->session->get($this->getName()); //Firstwewilltrytoloadtheuserusingtheidentifierinthesessionif //oneexists.Otherwisewewillcheckfora"rememberme"cookieinthis //request,andifoneexists,attempttoretrievetheuserusingthat. $user=null; if(!is_null($id)) { //provider为EloquentUserProvider $user=$this->provider->retrieveByID($id); } //Iftheuserisnull,butwedecrypta"recaller"cookiewecanattemptto //pulltheuserdataonthatcookiewhichservesasaremembercookieon //theapplication.Oncewehaveauserwecanreturnittothecaller. $recaller=$this->getRecaller(); if(is_null($user)&&!is_null($recaller)) { $user=$this->getUserByRecaller($recaller); } return$this->user=$user; }
更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。