在laravel5.4中使用自定義的字段登錄晰绎,注冊(cè)的過程
在使用laravel自帶的注冊(cè)登錄時(shí),默認(rèn)的登錄名是email,這個(gè)有的時(shí)候不能滿足需求括丁,下面是修改的步驟
- 首先想到使用php artisan route:list 查看現(xiàn)在項(xiàng)目的登錄荞下,注冊(cè)路由執(zhí)行的方法
|
| | GET|HEAD | admin/login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | admin/login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | POST | admin/logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | admin/logout | | App\Http\Controllers\Auth\LoginController@logout | web |
| | POST | admin/password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest |
| | GET|HEAD | admin/password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest |
| | POST | admin/password/reset | | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest |
| | GET|HEAD | admin/password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest |
| | GET|HEAD | admin/register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
| | POST | admin/register | | App\Http\Controllers\Auth\RegisterController@register | web,gues
找到 admin/login 路由對(duì)應(yīng)的方法有兩個(gè)
App\Http\Controllers\Auth\LoginController@showLoginForm
App\Http\Controllers\Auth\LoginController@login
第一個(gè)方法是展示給用戶登錄頁面,第二個(gè)是提交用戶填寫的登錄信息。這兩個(gè)方法都在App\Http\Controllers\Auth\LoginController 內(nèi)尖昏,然后我就打開這個(gè)文件發(fā)現(xiàn)沒有找到這兩個(gè)方法仰税,注意到LoginController使用了use AuthenticatesUsers;就進(jìn)入AuthenticatesUsers找這兩個(gè)方法抽诉。
public function showLoginForm() {
return view('auth.login');
}
這個(gè)方法可以改變登錄頁面
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.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
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);
}
順藤摸瓜找到
protected function validateLogin(Request $request) {
$this->validate($request, [
$this->username() => 'required', 'password' => 'required',
]);
}
然后
public function username() {
return 'email';
}
這里找到了登錄字段系統(tǒng)默認(rèn)返回的是email,本來直接將該字段修改為自己想要的字段就可以了陨簇,但是注意到該文件位于/vendor文件夾下,屬于第三方依賴不能直接修改迹淌。想到在引入trait的文件中重新定義和被引入trait文件中同名的方法時(shí)河绽,新方法會(huì)覆蓋掉trait中的方法。那么只要在LoginController中修改就可以了唉窃。
/**
* 登錄名
*/
protected $username = '自定義字段';
public function username() {
return $this->username;
}
這樣登錄的過程中就會(huì)使用自定義的字段了
登錄完成后同樣的去修改注冊(cè) 打開RegisterController ,修改下面兩個(gè)方法
protected function validator(array $data) {
return Validator::make($data, [
'自定義字段' => 'required|max:255',
'password' => 'required|min:6|confirmed',
]);
}
protected function create(array $data) {
return User::create([
'自定義字段' => $data['自定義字段'],
'password' => bcrypt($data['password']),
]);
}
修改完成后再去修改 登錄耙饰,注冊(cè)頁 表單里 用戶名 input的name, 修改成和自定義字段一直即可。