由於@Gaurav羅伊提到的,你必須首先通過鍵入在控制檯上php artisan make:auth
創建Laravel的認證途徑。您會注意到在Controllers
目錄中存在一個名爲Auth
的目錄,其中包含一些控制器。打開LoginController
並覆蓋username()
函數並返回希望用密碼進行身份驗證的列。你的情況:
private $value = 'username';
public function username()
{
return $this->value;
}
現在覆蓋attemptLogin(Request $request)
功能,並嘗試與用戶名或郵箱登錄:
protected function attemptLogin(Request $request)
{
// try to login with username
$loginWithUsername = $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
// if credentials with username are not valid, try with email
if (!$loginWithUsername) {
// replace username with email
$this->value = 'email';
$this->username();
// add input value to email
$request['email'] = $request['username'];
unset($request['username']);
return $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
}
return $loginWithUsername;
}
現在去resources->views->auth
,開放login
文件,並取代電子郵件的輸入,所以它可以接受的用戶名。例如:
來源:
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
要:
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
現在你可以用用戶名或郵箱登錄!
只需刪除'exit'? –
這是錯誤的留在那裏。我刪除它 – JohnB
是不是laravel已經有默認的中間件來處理身份驗證? –