1、首先確定重置密碼的路由
我們在安裝好laravel的時候默認生成的重置密碼是在用戶未登錄的情況下進行的。所以使用原來的控制器是不可行的,并且原有的重置密碼,并不需要查看原始密碼是否正確,而是通過郵件來進行直接更改密碼,所以控制器方法的話,我們也需要重新寫個。我們使用php artisan make:controller UserController
創建一個控制器類,然后創建兩條路由Route::get('reset', 'UserController@getReset')
和Route::post('reset', 'UserController@postReset')
。
前者是顯示一個重置密碼的頁面get請求,后面是重置密碼post請求。
2、顯示重置密碼頁
這個使用的是getReset
這個方法,這個方法只需要顯示一個視圖所以并沒有特別的邏輯
1
2
3
4
|
public function getReset() { return view( 'auth.reset' ); } |
3、請求重置密碼
這個使用的是postReset
這個方法,接收數據的話我們使用兩種方法接收傳過來的數據都可以:一種是使用request的方法接收數據,另外一種是使用Input::get的方法獲取數據。Request
的話需要引入use Illuminate\Http\Request
類,Input
的話需要引入use Input
類,這里我們選擇使用request
來接收。
4、驗證規則
驗證的話,laravel為我們提供了一套驗證的規則,使用validator
的Validator::make()
方法進行驗證
1
2
3
4
5
6
7
8
9
10
11
|
$data = $request ->all(); //接收所有的數據 $rules = [ 'oldpassword' => 'required|between:6,20' , 'password' => 'required|between:6,20|confirmed' , ]; $messages = [ 'required' => '密碼不能為空' , 'between' => '密碼必須是6~20位之間' , 'confirmed' => '新密碼和確認密碼不匹配' ]; $validator = Validator::make( $data , $rules , $messages ); |
$data
接收到從from傳過來的數據信息;
rules
對接收到的值進行判斷,其中數組前面的oldpassword
和password
是從前端from接收到的原始密碼和新密碼的name字段數據進行驗證;
驗證規則的話在手冊的驗證章節都有,值得注意的是,使用confirmed的話是為了新密碼和確認密碼進行相同判斷,確認密碼必須的name值必須是新密碼的name值后面加上'_confirmation'
,比如新密碼的name值為newpassword
的話,確認密碼的name值則必須為newpassword_confirmation
才可以進行判斷messages
對驗證的數據請求,顯示什么提示。
然后通過上面的驗證,還有個情況是沒有驗證的,那就是輸入的原始密碼是否和數據庫里的原始密碼相同。
這里我們可以先把這個用戶的信息從數據庫里給查出來,然后和輸入的原始密碼進行比對。這里我們使用Auth::user()
來獲取用戶的信息,這個方法需要引入use Auth;
類,然后通過Hash::check()
來進行密碼判斷。判斷完以后還有個問題,那就是,如何把錯誤信息給壓入到validator的錯誤信息里,這里laravel為我們提供了after方法:
1
2
3
4
5
6
7
8
9
10
11
|
$user = Auth::user(); $validator ->after( function ( $validator ) use ( $oldpassword , $user ) { if (!\Hash::check( $oldpassword , $user ->password)) { //原始密碼和數據庫里的密碼進行比對 $validator ->errors()->add( 'oldpassword' , '原密碼錯誤' ); //錯誤的話顯示原始密碼錯誤 } }); if ( $validator ->fails()) { //判斷是否有錯誤 return back()->withErrors( $validator ); //重定向頁面,并把錯誤信息存入一次性session里 } $user ->password = bcrypt( $password ); //使用bcrypt函數進行新密碼加密 $user ->save(); //成功后,保存新密碼 |
這里因為after
引入了一個PHP的匿名函數,所以我們需要使用use
關鍵字把外部數據給傳入到匿名函數里(PS:php新特性,閉包和匿名函數)
在匿名函數里我們引入了一個全局函數所以我們需要在函數前面加\(PS:php新特性,命名空間章節,全局命名空間)
5、前端顯示錯誤信息
前端顯示的話,我們使用$errors
變量來顯示錯誤,根據官方文檔說明,調用的是Illuminate\Support\MessageBag
的示例,有興趣的話,可以看下。我們使用count($errors) > 0
來判斷是否有錯誤,使用 $errors->first()
顯示一條錯誤信息:
1
2
3
4
5
6
|
@ if ( count ( $errors ) > 0) <div class = "alert alert-danger display-hide" style= "display: block;" > <button class = "close" data-close= "alert" ></button> <span> </span> </div> @ endif |
可能會有人問,如果我的錯誤不是顯示在固定的一個地方,而是在每個表單的后面顯示錯誤信息的話,這樣我們該怎么判斷和顯示呢? 答案是使用$errors->has('oldpassword')
來判斷有沒有這個名稱的錯誤,如果有的話,使用 $errors->first('oldpassword')
顯示這條錯誤:
1
2
3
4
5
6
|
@ if ( $errors ->has( 'oldpassword' ) ) <div class = "alert alert-danger display-hide" style= "display: block;" > <button class = "close" data-close= "alert" ></button> <span> </span> </div> @ endif |
其中oldpassword
是每個表單的里的name值,所以在使用after
方法添加自定義錯誤的時候 $validator->errors()->add('oldpassword', '原密碼錯誤');
中,oldpassword
一定要寫對是在哪個表單的錯誤,這樣才能正確的顯示。
6、完成后的示例
UserController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public function getReset() { return view( 'auth.reset' ); } public function postReset(Request $request ) { $oldpassword = $request ->input( 'oldpassword' ); $password = $request ->input( 'password' ); $data = $request ->all(); $rules = [ 'oldpassword' => 'required|between:6,20' , 'password' => 'required|between:6,20|confirmed' , ]; $messages = [ 'required' => '密碼不能為空' , 'between' => '密碼必須是6~20位之間' , 'confirmed' => '新密碼和確認密碼不匹配' ]; $validator = Validator::make( $data , $rules , $messages ); $user = Auth::user(); $validator ->after( function ( $validator ) use ( $oldpassword , $user ) { if (!\Hash::check( $oldpassword , $user ->password)) { $validator ->errors()->add( 'oldpassword' , '原密碼錯誤' ); } }); if ( $validator ->fails()) { return back()->withErrors( $validator ); //返回一次性錯誤 } $user ->password = bcrypt( $password ); $user ->save(); Auth::logout(); //更改完這次密碼后,退出這個用戶 return redirect( '/login' ); } |
reset.blade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
< form class = "login-form" action = "" method = "post" > < h3 class = "font-green" >修改密碼</ h3 > @if($errors->first()) < div class = "alert alert-danger display-hide" style = "display: block;" > < button class = "close" data-close = "alert" ></ button > < span > </ span > </ div > @endif {!! csrf_field() !!} < div class = "form-group" > < label class = "control-label visible-ie8 visible-ie9" >原始密碼</ label > < input class = "form-control placeholder-no-fix" type = "password" autocomplete = "off" placeholder = "Old Password" name = "oldpassword" > </ div > < div class = "form-group" > < label class = "control-label visible-ie8 visible-ie9" >新密碼</ label > < input class = "form-control placeholder-no-fix" type = "password" autocomplete = "off" id = "register_password" placeholder = "New password" name = "password" > </ div > < div class = "form-group" > < label class = "control-label visible-ie8 visible-ie9" >重復密碼</ label > < input class = "form-control placeholder-no-fix" type = "password" autocomplete = "off" placeholder = "Repeat password" name = "password_confirmation" > </ div > < div class = "form-actions" > < button type = "submit" id = "register-submit-btn" class = "btn btn-success uppercase pull-right" >確定</ button > </ div > </ form > |
總結
以上就是本文的全部內容,希望對大家學習使用Laravel有所幫助,如果有疑問的話歡迎留言討論。
原文:Dennis`s blog