- 上一節,我們完成了多字段登錄功能
- 這一節,我們來完成驗證碼功能
要使用驗證碼,我們就需要使用拓展包,可以理解爲插件,首先我們利用composer下載拓展吧
composer require mews/captcha
在文件config/app.php
中進行配置
'providers' => [
Mews\Captcha\CaptchaServiceProvider::class,
]
'aliases' => [
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
- 註冊拓展包
php artisan vendor:publish
我這裏使用了9
config/captcha.php
可以配置驗證碼的樣式,我這裏就使用默認樣式
在表單中使用,並給上刷新點擊
<img src="{{ captcha_src('flat') }}" style="cursor: pointer" onclick="this.src='{{captcha_src('flat')}}&'+Math.random()" >
現在我們不輸入驗證碼是可以直接登錄的,下面我們繼續改寫登錄
protected function validateLogin(Request $request)
{
$request->validate([
'captcha' => 'required|captcha',
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
-現在我們的驗證碼功能已經完成,只有輸入驗證碼且正確的情況下才能登錄成功