验证码
软件验证码采用 mews/captcha 扩展包实现
- 全局配置
config/app.php
'providers' => ServiceProvider::defaultProviders()->merge([ //... Mews\Captcha\CaptchaServiceProvider::class ])->toArray() 'aliases' => Facade::defaultAliases()->merge([ //... 'Captcha' => Mews\Captcha\Facades\Captcha::class, ])->toArray()
- 配置文件
config/captcha.php
- 数据接口
app/Http/Controllers/Login.php
use Mews\Captcha\Facades\Captcha; //验证码生成 api/login/captcha public function captcha(){ $captcha=Captcha::create('math',true); return ['state'=>'success','info'=>['key'=>$captcha['key'],'img'=>$captcha['img']]]; }
- 校验类型
名称 说明 default 默认 math 运算 flat 常规 mini 简易 inverse 反色 - 数据验证
math
$input=['key'=>'abc...','code'=>'13']; $rules = ['code' => 'required|captcha_api:'.$input['key'].',math']; $validator = validator()->make($input, $rules); if ($validator->fails()) { echo 'success'; }else{ echo 'error'; }