laravel 添加验证码
下载安装依赖包
composer require mews/captcha
生成配置文件 config/captcha.php
php artisan vendor:publish
[12] Provider: MewsCaptchaCaptchaServiceProvider
用例
扩展包提供了两个函数用于展示验证码:
captcha_img() - 返回 img 格式的验证码;
captcha_src() - 返回验证码的 url 地址。
在你的模板文件里(如:register.blade.php)直接调用即可:
<div class="form-group code">
<label>验证码</label>
<input class="tt-text" name="captcha">
{!! captcha_img() !!}
</div>
又或者:
<div class="form-group code">
<label>验证码</label>
<input class="tt-text" name="captcha">
<img src="{{captcha_src()}}">
</div>
判断用户输入的验证码是否正确
扩展包使用了 自定义验证规则 方式扩展了验证规则,我们只要在对应的 Controller 添加以下的规则即可:
protected function validateLogin(Request $request){
$this->validate($request, [
$this->username() => 'required',
'password' => 'required',
'captcha' => 'required|captcha',
],[
'captcha.required' => '验证码不能为空',
'captcha.captcha' => '请输入正确的验证码',]
);
}
以上。
相关文章