命令运行:composer require mews/captcha
使用Captcha服务提供者之前还需要在config/app.php
中注册服务提供者:
'providers' => [ // ... Mews\Captcha\CaptchaServiceProvider::class, ]
'aliases' => [ // ... 'Captcha' => Mews\Captcha\Facades\Captcha::class, ]
命令运行:php artisan vendor:publish 完成输入0
在控制器中:
use Validator;
use Gregwar\Captcha\CaptchaBuilder;
$rules = ['captcha' => 'required|captcha'];
$validator = Validator::make($request->all(), $rules);
if($validator->fails()) return view('Alert',['msg'=>'验证码错误']);
在视图中
<img src="{{url('Admin/Code?r='.time())}}" alt="验证码" class="pic" width="80px" height="38px" style="height:44px;cursor:pointer;" οnclick="this.src='{{url('Admin/Code?r=1')}}'+Math.random()"/>
所需路由
Route::get('Code','Admin\LoginController@getVerifyCode'); // 验证码
// 登录验证码 public function getVerifyCode(Request $request) { return captcha(); }