檢視中我將待驗證欄位命名為 account
。
resources/views/auth/login.blade.php
<form action="{{ route('login') }}" method="post">
{{ csrf_field() }}
{{--賬號--}}
<div class="form-group has-feedback">
<input type="text" name="account" class="form-control" placeholder="使用者名稱/郵箱/手機號">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
@if ($errors->has('account'))
<span class="help-block" style="color: #d73925;">
<strong>{{ $errors->first('account') }}</strong>
</span>
@endif
</div>
.
.
.
接下來在 LoginController.php
重寫 AuthenticatesUsers
trait 中的 attemptLogin()
、validateLogin()
、username()
、三個方法。
attemptLogin()
方法:
將
return $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
替換為
return collect(['username', 'email', 'phone'])->contains(function ($value) use ($request) {
$account = $request->get($this->username());
$password = $request->get('password');
return $this->guard()->attempt([$value => $account, 'password' => $password], $request->filled('remember'));
});
validateLogin()
方法:
主要是將 $this->username()
改為中文。當然你還可以新增自己的驗證規則。
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
'captcha' => 'required|captcha',
],[
'captcha.required' => ':attribute 不能為空',
'captcha.captcha' => '請輸入正確的 :attribute',
],[
$this->username() => '賬號',
'captcha' => '驗證碼',
]);
}
username()
方法:
不用說,將返回欄位改為 account
。
public function username()
{
return 'account';
}
完整程式碼:
app/Http/Controllers/Auth/LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Attempt to log the user into the application.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function attemptLogin(Request $request)
{
return collect(['username', 'email', 'phone'])->contains(function ($value) use ($request) {
$account = $request->get($this->username());
$password = $request->get('password');
return $this->guard()->attempt([$value => $account, 'password' => $password], $request->filled('remember'));
});
}
/**
* 登入驗證.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
'captcha' => 'required|captcha',
],[
'captcha.required' => ':attribute 不能為空',
'captcha.captcha' => '請輸入正確的 :attribute',
],[
$this->username() => '賬號',
'captcha' => '驗證碼',
]);
}
public function username()
{
return 'account';
}
}
最後還有一個小問題,複製 overtrue/laravel-lang 語言包稍作一個小修改:
$ mkdir resources/lang/zh-CN
$ cp vendor/caouecs/laravel-lang/src/zh-CN/auth.php resources/lang/zh-CN
將 'failed' => '使用者名稱或密碼錯誤。',
修改為 'failed' => '賬號或密碼錯誤。',
。
resources/lang/zh-CN/auth.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used during authentication for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
|
*/
'failed' => '賬號或密碼錯誤。',
'throttle' => '您的嘗試登入次數過多,請 :seconds 秒後再試。',
];
感謝 @BradStevens、@bing8u 指正。
本作品採用《CC 協議》,轉載必須註明作者和本文連結