Laravel框架用户登陆身份验证实现方法详解

本文实例讲述了Laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下:

laravel中检测用户是否登录,有以下的代码:

Auth::guest是如何调用的呢?

laravel用了Facade模式,相关门面类在laravel/framework/src/Illuminate/Support/Facades文件夹定义的,看下Auth类的定义:

laravel框架中,Facade模式使用反射,相关方法其实调用

AuthServiceProvider::register方法会注册:

app->bindShared('auth',function($app) { // Once the authentication service has actually been requested by the developer // we will set a variable in the application indicating such. This helps us // know that we need to set any queued cookies in the after event later. $app['auth.loaded'] = true; return new AuthManager($app); });

那为什么最终会调到哪里呢,看下堆栈:

guest() Illuminate\Support\Manager->__call public function __call($method,$parameters) { return call_user_func_array(array($this->driver(),$method),$parameters); }

看下driver的代码:

getDefaultDriver(); // If the given driver has not been created before,we will 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]; }

没有会调用

app['config']['auth.driver']; }

最终调用的是配置文件中配置的driver,如果配的是

'eloquent'

则调用的是

createEloquentProvider(); return new Guard($provider,$this->app['session.store']); }

所以Auth::guest最终调用的是Guard::guest方法

这里的逻辑先从session中取用户信息,奇怪的是session里只保存的是用户ID,然后拿这个ID来从数据库中取用户信息

loggedOut) return; // If we have already retrieved the user for the current request we can just // return it back immediately. We do not 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 will try to load the user using the identifier in the session if // one exists. Otherwise we will 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为EloquentUserProvider $user = $this->provider->retrieveByID($id); } // If the user is null,but we decrypt a "recaller" cookie we can attempt to // pull the user data on that cookie which serves as a remember cookie on // the application. Once we have a user we can return it to the caller. $recaller = $this->getRecaller(); if (is_null($user) && ! is_null($recaller)) { $user = $this->getUserByRecaller($recaller); } return $this->user = $user; }

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何利用Laravel实现数据缓存功能
Laravel权限功能的实战应用:如何实现用户组织架构权限控制
如何在Laravel中实现基于权限的多语言支持
掌握Laravel控制台命令,利用参数传递的力量
如何利用Laravel实现数据分页和搜索功能
如何在Laravel中使用中间件进行日志记录
如何在Laravel中使用中间件进行数据迁移
如何在Laravel中使用中间件进行数据统计
如何利用Laravel实现邮件发送和接收功能
如何在Laravel中使用中间件进行数据导出
Laravel权限功能详解:如何定义和管理用户角色
如何在Laravel中使用中间件进行用户反馈
如何使用Laravel开发一个基于微信公众号的在线点餐系统
Laravel权限功能的可靠性保证:如何实现权限的冗余备份和恢复
Laravel权限功能的进阶应用:如何实现权限的可视化管理和配置
如何在Laravel中使用中间件进行数据加密传输
Laravel权限功能的最佳实践:如何实现权限缓存和性能优化
如何在Laravel中使用中间件进行API认证
如何在Laravel中使用中间件进行数据加速
如何利用Laravel实现数据验证和过滤功能