Rumah > pembangunan bahagian belakang > tutorial php > 关于Laravel5.4实现多字段登录的方法

关于Laravel5.4实现多字段登录的方法

不言
Lepaskan: 2023-04-01 06:32:02
asal
1102 orang telah melayarinya

这篇文章主要介绍了关于Laravel5.4实现多字段登录的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

最近在工作中遇到一个需求,需要实现多字段登录的一个效果,就是可以使用手机或者邮箱任一种方式的登录,现在将解决的过程分享出来,所以这篇文章主要给大家介绍了基于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([&#39;email&#39; => $email, &#39;password&#39; => $password])) {
   // Authentication passed...
   return redirect()->intended(&#39;dashboard&#39;);
  }
 }
}
Salin selepas log masuk

这个方法会根据你传入的参数判断数据库中是否存在与之相匹配的用户,如果存在并且密码正确返回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&#39;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);
 }
Salin selepas log masuk

分析了一波这个文件,发现主要进行登录判断的就是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(&#39;remember&#39;)
  );
 }
Salin selepas log masuk

在LoginController重写后:

public function attemptLogin(Request $request)
 {
  $username = $request->input(&#39;username&#39;);
  $password = $request->input(&#39;password&#39;);

  // 验证用户名登录方式
  $usernameLogin = $this->guard()->attempt(
   [&#39;username&#39; => $username, &#39;password&#39; => $password], $request->has(&#39;remember&#39;)
  );
  if ($usernameLogin) {
   return true;
  }

  // 验证手机号登录方式
  $mobileLogin = $this->guard()->attempt(
   [&#39;mobile&#39; => $username, &#39;password&#39; => $password], $request->has(&#39;remember&#39;)
  );
  if ($mobileLogin) {
   return true;
  }

  // 验证邮箱登录方式
  $emailLogin = $this->guard()->attempt(
   [&#39;email&#39; => $username, &#39;password&#39; => $password], $request->has(&#39;remember&#39;)
  );
  if ($emailLogin) {
   return true;
  }

  return false;
 }
Salin selepas log masuk

只需要用attempt方法进行多次判断即可,只要成功就返回true,不成功继续用其他字段进行判断,都不成功则返回flase

测试,可以实现多字段登录效果

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

Larave如何l实现短信注册

使用Laravel实现定时任务的方法

Atas ialah kandungan terperinci 关于Laravel5.4实现多字段登录的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Isu terkini
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan