美文网首页
laravel 注册验证码

laravel 注册验证码

作者: Jason_lai | 来源:发表于2018-04-22 11:06 被阅读0次

使用第三方扩展包 mews/captcha 作为基础来实现 Laravel 中的验证码功能

  • 使用composer安装扩展包
composer require "mews/captcha:~2.0"
  • 运行以下命令生成配置文件 config/captcha.php
php artisan vendor:publish --provider='Mews\Captcha\CaptchaServiceProvider'
  • 可以在config/captcha.php中修改验证码的长度、背景颜色、文字颜色等属性。

页面嵌入

  • 前端展示 —— 生成验证码给用户展示,并收集用户输入的答案
<div class="form-group {{ $errors->has('captcha') ? ' has-error' : '' }}">
  <label for="captcha" class="col-md-4 control-label">验证码</label>

  <div class="col-md-6">
    <input id="captcha" class="form-control" name="captcha" >

    <img class="thumbnail captcha" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码">

       @if ($errors->has('captcha'))
              <span class="help-block">
                 <strong>{{ $errors->first('captcha') }}</strong>
              </span>
       @endif
    </div>
 </div>
  • 后端验证 —— 接收答案,检测用户输入的验证码是否正确
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:6|confirmed',
            'captcha' => 'required|captcha',
        ], [
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '请输入正确的验证码',
        ]);
    }

相关文章

网友评论

      本文标题:laravel 注册验证码

      本文链接:https://www.haomeiwen.com/subject/fcyqlftx.html