发送验证码
一.安装验证码 composer 扩展包#
- 安装扩展包:
composer require mews/captcha
- 生成配置文件,并选择 Mews\Captcha\CaptchaServiceProvider 代表的数字回车
php artisan vendor:publish
- 修改 config/captcha.php , 修改 flat 长度为 4 位
- 修改注册模板,添加验证码
<div class="form-group row"> <label for="captcha" class="col-md-4 col-form-label text-md-right">验证码</label> <div class="col-md-6"> <input id="captcha" class="form-control{{ $errors->has('captcha') ? ' is-invalid' : '' }}" name="captcha" required> <img class="thumbnail captcha mt-3 mb-2" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码"> @if ($errors->has('captcha')) <span class="invalid-feedback" role="alert"> <strong>{{ $errors->first('captcha') }}</strong> </span> @endif </div> </div>
- 后端添加验证码 captcha 验证
protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], 'captcha' => ['required', 'captcha'], ], [ 'captcha.required' => '验证码不能为空', 'captcha.captcha' => '请输入正确的验证码', ]); }
- 预览效果