美文网首页
验证码 mews/captcha

验证码 mews/captcha

作者: 熊航 | 来源:发表于2019-05-13 14:59 被阅读0次

安装

安装扩展包

composer require "mews/captcha:~2.0"

生成配置文件 config/captcha.php

php artisan vendor:publish --provider='Mews\Captcha\CaptchaServiceProvider'

页面嵌入

前端展示
<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>

代码解释:

  1. captcha_src() 方法是 mews/captcha 提供的辅助方法,用于生成验证码图片链接;
  2. 『验证码』区块中 onclick() 是 JavaScript 代码,实现了点击图片重新获取验证码的功能,允许用户在验证码太难识别的情况下换一张图片试试。
后端验证
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'captcha' => ['required', 'captcha'],
        ], [
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '请输入正确的验证码',
        ]);
    }

相关文章

网友评论

      本文标题:验证码 mews/captcha

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