Laravel框架用户登陆身份验证实现方法详解
时间:2019-04-06
本文章向大家介绍Laravel框架用户登陆身份验证实现方法详解,主要包括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相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- 【JMeter-4】JMeter关联:JMeter正则表达式提取器与JSON提取器
- 【JMeter系列-5】JMeter操作Mysql数据库
- 【JMeter系列-6】JMeter BeanShell Sampler与JMeter BeanShell断言
- 【JMeter系列-7】Linux下执行测试
- 【JMeter系列-8】JMeter自定义日志与日志分析
- 【JMeter系列-9】 JMeter常用内置对象
- 【JMeter系列-10】JMeter websocket接口测试
- [Mysql-2] 远程连接数据库错误:host 'xxx.xxx' is blocked
- Linux超能力BPF技术介绍及学习分享(技术创作101训练营)
- 已创建好的vue项目引入vuex
- 【Mysql学习之旅-2】经典sql面试题及答案分析
- 【Mysql-3】条件判断函数-CASE WHEN、IF、IFNULL详解
- Linux设备树语法详解
- 【Mysql进阶-2】图文并茂说尽Mysql索引
- 1.8W字|了不起的 TypeScript 入门教程(第二版)