Laravel框架用户登陆身份验证实现方法详解
作者:szphper 发布时间:2024-04-30 08:47:12
标签:Laravel框架,登陆验证
本文实例讲述了Laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下:
laravel中检测用户是否登录,有以下的代码:
if ( !Auth::guest() )
{
return Redirect::to('/dashboard');
}
那Auth::guest
是如何调用的呢?
laravel用了Facade模式,相关门面类在laravel/framework/src/Illuminate/Support/Facades文件夹定义的,看下Auth类的定义:
class Auth extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'auth'; }
}
laravel框架中,Facade模式使用反射,相关方法其实调用app['auth']中的方法,app['auth']是什么时候创建的呢,
AuthServiceProvider::register
方法会注册:
$this->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);
});
那为什么最终会调到哪里呢,看下堆栈:
Illuminate\Support\Facades\Auth::guest()
Illuminate\Support\Facades\Facade::__callStatic
Illuminate\Auth\AuthManager->guest()
Illuminate\Support\Manager->__call
public function __call($method, $parameters)
{
return call_user_func_array(array($this->driver(), $method), $parameters);
}
看下driver的代码:
public function driver($driver = null)
{
$driver = $driver ?: $this->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];
}
没有会调用getDefaultDrive方法
/**
* Get the default authentication driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->app['config']['auth.driver'];
}
最终调用的是配置文件中配置的driver,如果配的是
'driver' => 'eloquent'
则调用的是
public function createEloquentDriver()
{
$provider = $this->createEloquentProvider();
return new Guard($provider, $this->app['session.store']);
}
所以Auth::guest
最终调用的是Guard::guest
方法
这里的逻辑先从session中取用户信息,奇怪的是session里只保存的是用户ID,然后拿这个ID来从数据库中取用户信息
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 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框架的PHP程序设计有所帮助。
来源:http://www.cnblogs.com/szprg/articles/4592426.html


猜你喜欢
- SQL 联合查询与XML解析实例
- 说明1、通过特征本身的方差来筛选特征。特征的方差越小,特征的变化越不明显。2、变化越不明显的特征对我们区分标签没有太大作用,因此应该消除这些
- 多元正态分布(多元高斯分布)直接从多元正态分布讲起。多元正态分布公式如下:这就是多元正态分布的定义,均值好理解,就是高斯分布的概率分布值最大
- 希望达到的效果工具类的Golang项目需要编译成二进制文件后在命令行中运行,所以希望在github里面创建一个新的release后能自动编译
- JQuery Solar System 是一个用JQuery写的东西,效果真是cool ! 没想到使用Javascript 也可以做到这种效
- 前言Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出。中间件的设计
- MongoDB是一个文档型数据库,是NOSQL家族中最重要的成员之一,以下代码封装了MongoDB的基本操作。MongoDBConfig.j
- 在腾讯云上面搭建的mysql使用开发的电脑上navicat进行访问时总是特别的慢,原来是Mysql会对请求的地址进行域名解析,开发的电脑并没
- 在pyhton中,经常会用到input()语句,但是input()语句输入的内容只能时字符串类型,而我们经常要输入int类型的数据等,那么就
- 复制代码CREATE FUNCTION fGetStrBySplit ( @Source VARCHAR(max), @Index INT,
- 最近一直在做Dnn模块的开发,过程中碰到这么一个问题,需要同时插入N条数据,不想在程序里控制,但是SQL Sever又不支持数组参数.所以只
- 在用python进行图像处理时,有时需要遍历numpy数组,下面是遍历数组的方法:[rows, cols] = num.shape for
- 1.if判断1.1 if语句if 表达式:语句1语句2……1.“if&
- php数组中元素的存在方式是以键值对的方式('key'=>'value'),有时候我们需要根据键删除数
- Pytorch损失函数torch.nn.NLLLoss()在各种深度学习框架中,我们最常用的损失函数就是交叉熵(torch.nn.Cross
- JavaScript游戏开发之键盘控制层的移动截图:<html> <head> <meta http-equi
- 很多网站在注册时除了需要用户填写用户名与密码之外,还会要求用户输入邮箱,而且是属于那种不填写就不能完成注册的强制型的。碰到这种情况的时候,一
- 前言ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在2015年6月正式发布了。它的目标,是
- 本文实例讲述了Python3爬虫学习之爬虫利器Beautiful Soup用法。分享给大家供大家参考,具体如下:爬虫利器Beautiful
- 《lnmp一键安装包》中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器