一、安装captchacomposer require mews/captcha
二、配置[/config/app.php]添加以下配置'providers' => [
// ...
Mews\Captcha\CaptchaServiceProvider::class,
]
'aliases' => [
// ...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
]
三、生成自定义配置文件php artisan vendor:publish
运行之后,就可以在 config/captcha.php 中进行配置了。
四、使用 Captcha 为 auth 组件添加验证码功能
1、在登录视图中增加验证码的选项,可以加到密码和 remember me 之间/resources/views/auth/login.blade.php
验证码
@if($errors->has('captcha'))
{{$errors->first('captcha')}}
@endif
2、重写AuthController 登录验证方法,并自定义提示信息: 修改 App\Http\Controllers\Auth\LoginController#首先要引入如下代码:use Illuminate\Http\Request;
#重写validateLogin方法:
#在验证里面加入验证码的规则验证即可/**
* DESC: 重写 AuthenticatesUsers 登录验证方法,并自定义提示信息;
* 原验证方法 Illuminate\Foundation\Auth\AuthenticatesUsers
* @param Request $request
*/
protected function validateLogin(Request $request){
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string'
'captcha' => 'required|captcha',
],[
'captcha.required' => '请填写验证码',
'captcha.captcha' => '验证码错误',
]);
}
完成后,刷新登录页面即可完成安装了