laravel-验证码

1
composer require mews/captcha

配置

在config/app.php中注册服务提供者

1
2
3
4
'providers' => [
// ...
MewsCaptchaCaptchaServiceProvider::class,
]

注册门面

1
2
3
4
'aliases' => [
// ...
'Captcha' => MewsCaptchaFacadesCaptcha::class,
]

生成验证码配置文件

1
php artisan vendor:publish

然后就会在config中出现一个captcha.php文件,我们可以修改这个文件来设置验证码相关参数

1
2
3
4
Route::any('captcha', function()
{
return captcha();
});

html代码

1
<img src="/captcha" onclick="this.src='/captcha?'+Math.random()">

判断验证码

1
2
3
4
5
6
7
$rules = ['captcha' => 'required|captcha'];
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
echo '错误';
}else{
echo '正确';
}