基于Laravel5.4实现多字段登录功能方法示例
前言
最近在一个项目中需要实现一个多字段登录功能,简单来说就是可以使用用户名、邮箱或手机号任意一种方式进行登录。所以本文就来给大家介绍了关于Laravel5.4多字段登录的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍吧。
以下内容基于laravel5.4
方法如下:
首先,通过artisan工具生成auth模块
php artisan make:auth
这时候App\Http\Controllers目录下会新增一个Auth目录,该目录下为注册登录相关的控制器,resources\views目录下也会生成一些与注册登录相关的视图
laravel的官方文档中说手动认证用户需要使用Illuminate\Support\Facades\Auth类的attempt方法,如下:
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Auth; class LoginController extends Controller { /** * Handle an authentication attempt. * * @return Response */ public function authenticate() { if (Auth::attempt(['email' => $email, 'password' => $password])) { // Authentication passed... return redirect()->intended('dashboard'); } } }
这个方法会根据你传入的参数判断数据库中是否存在与之相匹配的用户,如果存在并且密码正确返回true,反之返回false
遂在LoginController中添加该方法,但是好像并没有效果
于是开始观察LoginController的实现机制,发现它实现了一个AuthenticatesUsers的trait,追踪到这个trait的定义文件,发现这个文件就是我们想要的东西
里面有一个login方法,就是负责处理登录的逻辑
/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response */ public function login(Request $request) { // 表单验证 $this->validateLogin($request); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. // 防止暴力破解,多次登录失败会根据IP锁定 if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } // 这个就是主要的负责判断数据库中是否存在相应的账号和密码的地方,我们需要重写的就是attemptLogin方法 if ($this->attemptLogin($request)) { return $this->sendLoginResponse($request); } // If the login attempt was unsuccessful we will increment the number of attempts // to login and redirect the user back to the login form. Of course, when this // user surpasses their maximum number of attempts they will get locked out. // 登录失败,失败次数++,防止暴力破解 $this->incrementLoginAttempts($request); // 返回失败响应 return $this->sendFailedLoginResponse($request); }
分析了一波这个文件,发现主要进行登录判断的就是attemptLogin方法,我们只要重写这个方法即可,先看看原来的是怎么写的,根据原来的进行重写:
/** * Attempt to log the user into the application. * * @param \Illuminate\Http\Request $request * @return bool */ protected function attemptLogin(Request $request) { return $this->guard()->attempt( $this->credentials($request), $request->has('remember') ); }
在LoginController重写后:
public function attemptLogin(Request $request) { $username = $request->input('username'); $password = $request->input('password'); // 验证用户名登录方式 $usernameLogin = $this->guard()->attempt( ['username' => $username, 'password' => $password], $request->has('remember') ); if ($usernameLogin) { return true; } // 验证手机号登录方式 $mobileLogin = $this->guard()->attempt( ['mobile' => $username, 'password' => $password], $request->has('remember') ); if ($mobileLogin) { return true; } // 验证邮箱登录方式 $emailLogin = $this->guard()->attempt( ['email' => $username, 'password' => $password], $request->has('remember') ); if ($emailLogin) { return true; } return false; }
只需要用attempt方法进行多次判断即可,只要成功就返回true,不成功继续用其他字段进行判断,都不成功则返回flase
测试,可以实现多字段登录效果
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
- React Native 0.50版本新功能简介
- Android滤镜效果实现及原理分析
- Spring Boot入门及整合mybatis
- Swift开发React Native组件
- 深入理解Android插件化技术
- Kotlin DSL详解
- 大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践
- 事务与一致性:刚性or柔性?
- 文本相似度算法小结
- return的值都去哪了?去哪了,“谁伸手了,return的结果就给谁”
- 面试时对方问你,“xxx需求你是怎么做的”?你可以这样回答
- vue.js的条件渲染,其实就是模板里面写if else
- vue.js的插槽 - slot 是啥?要我说,它就是个“形参”
- Vue2.0,lifeCycle ['laɪfˌsaɪkl] -- 生命周期大白话~
- 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
- 树莓派基础实验24:超声波测距传感器实验
- 数据结构与算法系列2 线性表 链表的分类+使用java实现链表+链表源码详解
- 从spring boot 启动过程看apollo的初始化过程( 二)
- Apache IoTDB 系列教程-4:客户端接口
- 数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解
- Kali中密码暴力破解工具hydra的使用
- Apache IoTDB 系列教程-8:文件同步工具
- 树莓派基础实验25:DS18B20温度传感器实验
- 迈出加入 Apache IoTDB 社区的第一步!(订阅邮件、调试代码)
- kali下一些代理工具的简单描述
- 树莓派基础实验26:旋转编码器实验
- java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用)
- JAVA创建对象有哪几种方式
- Apache IoTDB 系列教程-7:时序数据文件格式 TsFile
- XSS原理及代码分析