laravel入門實戰開發(六):登錄驗證碼驗證登錄

  • 上一節,我們完成了多字段登錄功能
  • 這一節,我們來完成驗證碼功能
    要使用驗證碼,我們就需要使用拓展包,可以理解爲插件,首先我們利用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',
    ]);

}

-現在我們的驗證碼功能已經完成,只有輸入驗證碼且正確的情況下才能登錄成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章