Laravel 实现多字段登录的三种方法
下面三种方法基本原理都是相同的。
1. 方法一
使用 Laravel 自带认证系统,修改 /app/Http/Controllers/Auth/AuthController.php 文件,重写方法(原方法所在文件 /vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php):
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3878')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3878>namespace App\Http\Controllers\Auth;
......
use Illuminate\Http\Request; // 增加该行
class AuthController extends Controller
{
protected $username = 'login';
....
protected function getCredentials(Request $request)
{
$login = $request->get('login');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
return [
$field => $login,
'password' => $request->get('password'),
];
}
}
2. 方法二
修改 /app/Http/Controllers/Auth/AuthController.php 文件,这也是使用 Laravel 自带认证系统的一种方法。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4668')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4668>namespace App\Http\Controllers\Auth;
......
use Illuminate\Http\Request; // 增加该行
class AuthController extends Controller
{
// 修改这里
use AuthenticatesAndRegistersUsers, ThrottlesLogins {
AuthenticatesAndRegistersUsers::postLogin as laravelPostLogin;
}
......
// 增加方法
public function postLogin(Request $request)
{
$field = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
$request->merge([$field => $request->input('login')]);
$this->username = $field;
return self::laravelPostLogin($request);
}
}
3. 方法三
重写登录功能
LoginRequest.php:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9196')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9196>public function rules()
{
return [
'login' => 'required',
'password' => 'required'
];
}
AuthController.php:
public function login(LoginRequest $request)
{
$field = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
$request->merge([$field => $request->input('login')]);
if ($this->auth->attempt($request->only($field, 'password')))
{
return redirect('/');
}
return redirect('/login')->withErrors([
'error' => 'These credentials do not match our records.',
]);
}
您可能感兴趣的文章:
Laravel 实现多字段登录的三种方法
搭建php Laravel框架教程详解
Laravel HTTP路由基本使用及路由参数
Laravel 集成的 Monolog 库对日志进行配置和记录实例
Laravel 5 中集成 Pjax 实现无刷新加载页面案例
Laravel 5.3 cache()函数用法介绍
Laravel框架 HTTP路由CSRF攻击原理及其防护例子
swoole laravel 区别
Laravel 5.2 安装配置教程详解
Laravel 5.3安装配置用户手册