Laravel框架用户登陆身份验证实现方法详解
作者:szphper 时间:2024-04-30 08:47:12
本文实例讲述了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
标签:Laravel框架,登陆验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django-silk性能测试工具安装及使用解析
2023-12-08 08:53:19
PHP获取当前相对于域名目录的方法
2023-08-19 18:47:31
浅谈python中的错误与异常
2021-11-06 11:51:23
网易网站设计(思想)
2009-03-27 17:51:00
![](https://img.aspxhome.com/file/UploadPic/20093/27/01-38s.jpg)
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
2022-12-24 23:49:30
使用Python构建Hopfield网络的教程
2022-12-14 01:27:52
![](https://img.aspxhome.com/file/2023/8/127728_0s.jpg)
详解vue中的computed的this指向问题
2024-04-27 15:46:56
![](https://img.aspxhome.com/file/2023/7/133157_0s.png)
浏览器针对单服务器连接数问题
2008-05-12 22:27:00
ASP关于SQL插入数据后获得当前ID
2010-01-24 19:55:00
Python函数式编程指南(三):迭代器详解
2023-06-03 06:11:52
Python中实现参数类型检查的简单方法
2022-07-26 18:53:07
python 使用cycle构造无限循环迭代器
2022-08-19 13:09:10
![](https://img.aspxhome.com/file/2023/9/89359_0s.png)
使用Python来做一个屏幕录制工具的操作代码
2023-11-02 18:57:40
![](https://img.aspxhome.com/file/2023/8/62948_0s.gif)
python3 实现除法结果为整数
2023-06-27 20:08:22
![](https://img.aspxhome.com/file/2023/6/118546_0s.jpg)
Python解决pip install时出现的Could not fetch URL问题
2023-08-01 14:24:45
![](https://img.aspxhome.com/file/2023/7/61997_0s.png)
讲解使用SQL Server升级顾问的详细步骤
2009-01-04 14:14:00
Python计算双重差分模型DID及其对应P值使用详解
2022-08-06 21:07:53
![](https://img.aspxhome.com/file/2023/5/68335_0s.jpg)
超详细注释之OpenCV构建透明的叠加层
2021-08-18 14:51:01
![](https://img.aspxhome.com/file/2023/1/134431_0s.gif)
Mysql json类型字段Java+Mybatis数据字典功能的实践方式
2024-01-22 00:24:34
![](https://img.aspxhome.com/file/2023/3/71243_0s.jpg)
vue axios 二次封装的示例代码
2024-05-28 15:55:04