https://mp.weixin.qq.com/s/PO5f5OJPt5FzUZr-7Xz8-g
Laravel實現找回密碼及密碼重置功能在php實現與在這里實現會有什么區別呢,下面我們來看看Laravel中的例子,在php中就不介紹了大家都懂的。
忘記密碼是應用中常見的場景之一,Laravel5也提供了對密碼重置的支持,我們只需稍微做一下配置即可輕松實現重置密碼。
1、實現思路
通過給用戶注冊郵箱發送包含特定令牌的重置密碼鏈接,然后用戶登錄郵箱通過訪問該重置密碼鏈接實現密碼的重置。
2、數據表&模型
實現CanResetPasswordContract契約並使用CanResetPasswordtrait的User模型(Laravel自帶)
用於存放重置密碼令牌的表password_resets(Laravel自帶該表對應遷移文件,上一節中已一並創建)
3、創建路由
Laravel自帶了用於密碼重置的控制器Auth\PasswordController,和上一節提到的AuthController位於統一目錄下。重置密碼相關的業務邏輯都是通過該控制器中使用的ResetsPasswordstrait來實現的。下面我們在routes.php中為重置密碼定義相關路由規則:
// 發送密碼重置鏈接路由 Route::get('password/email', 'Auth\PasswordController@getEmail'); Route::post('password/email', 'Auth\PasswordController@postEmail'); // 密碼重置路由 Route::get('password/reset/{token}', 'Auth\PasswordController@getReset'); Route::post('password/reset', 'Auth\PasswordController@postReset');
4、創建視圖
定義好路由之后我們為get請求定義對應視圖文件,首先創建發送密碼重置鏈接路由對應視圖resources/views/auth/password.blade.php:
<form method="POST" action="/password/email"> {!! csrf_field() !!} <div> Email <input type="email" name="email" value="{{ old('email') }}"> </div> <div> <button type="submit"> 發送密碼重置鏈接 </button> </div> </form>
然后創建重置密碼路由對應視圖resources/views/auth/reset.blade.php:
<form method="POST" action="/password/reset"> {!! csrf_field() !!} <input type="hidden" name="token" value="{{ $token }}"> <div> Email:<input type="email" name="email" value="{{ old('email') }}"> </div> <div> 新密碼:<input type="password" name="password"> </div> <div> 確認密碼:<input type="password" name="password_confirmation"> </div> <div> <button type="submit"> 重置密碼 </button> </div> </form>
此外我們還要創建一個額外視圖——發送密碼重置鏈接的郵件模板視圖resources/views/emails/password.blade.php,用於為該郵件提供視圖模板:
點擊這里重置密碼: {{ url('password/reset/'.$token) }}
如果該郵件模板視圖文件路徑位於其他地方,不要忘了配置config/auth.php中的password.email值與新路徑對應。
5、發送郵件配置
接下來我們要做的是配置相關文件實現郵件發送功能為下一步測試做准備。
Laravel使用SwiftMailer庫提供的郵件API實現郵件操作,詳情可查看郵件文檔,這里我們僅作簡單配置實現郵件發送,郵件配置文件是config/mail.php:
<?php return [ 'driver' => env('MAIL_DRIVER', 'smtp'), 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('MAIL_PORT', 587), 'from' => ['address' => null, 'name' => null], 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'sendmail' => '/usr/sbin/sendmail -bs', 'pretend' => false, ];
可見大部分配置在.env文件中設置,這里我的.env文件配置如下:
MAIL_DRIVER=smtp MAIL_HOST=smtp.163.com MAIL_PORT=25 MAIL_USERNAME=yaojinbu@163.com MAIL_PASSWORD=mypassword MAIL_ENCRYPTION=null
我使用的是163郵箱,其它郵箱參考對應郵箱相關設置項,並將自己的賬戶信息填寫到MAIL_USERNAME和MAIL_PASSWORD。
此外我們還要配置mail.php中的from配置如下:
'from' => ['address' => 'yaojinbu@163.com ', 'name' => 'Laravel學院'],
這里只需要將address和.env文件中的MAIL_USERNAME值相匹配即可。至於name值就是郵箱中發件人名稱,可自定義。
做好這一步配置后,接下來即可測試密碼重置了。
6、重置密碼
在瀏覽器中訪問http://laravel.app:8000/password/email,頁面顯示如下:
Laravel 發送重置密碼郵件
在Email輸入框中填寫你的注冊郵箱,點擊“發送密碼重置鏈接”,然后去郵箱中查看收件箱,如果發送成功,可收到一封內容如下的密碼重置郵件:
點擊這里重置密碼:
http://laravel.app:8000/password/reset/96c652e4885591c7ecfcb4f1ecc6f9b877ac1a2ab445e7fb45a89fdfc7283585
此時數據表password_resets中也新增了一條記錄,用於保存重置鏈接令牌:
Laravel保存重置密碼token
該令牌默認保存一個小時,要修有效期,可通過編輯config/auth.php中password.expire來實現。
我們將重置密碼郵件中的鏈接拷貝出來粘貼到瀏覽器地址欄回車,頁面顯示內容如下:
Laravel重置密碼頁面
填寫該表單后點擊重置密碼按鈕即可完成密碼重置。
密碼重置成功后,默認跳轉鏈接為/home,我們可以在PasswordController中通過設置$redirectTo/$redirectPath屬性的值修改該跳轉鏈接:
protected $redirectPath = '/profile';
這樣重置成功后會跳轉到http://laravel.app:8000/profile:
test登錄成功!
同時password_resets中的對應記錄也會被刪除。
好了,下次登錄就可以使用新密碼進行登錄認證了。