No CAPTCHA reCAPTCHA For Laravel。

3.1.0 2022-01-28 11:03 UTC

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

recaptcha_anchor 2x

这是对anhskohbo/no-captcha的分支,看起来不再被支持了!

对于Laravel 4,请使用v1分支。

安装

composer require DutchCodingCompany/no-captcha

Laravel 5及以上

设置

注意 此包支持Laravel 5.5及以上版本的自动发现功能,因此如果您使用的是Laravel 5.5及以上版本,请跳过这些设置说明。

app/config/app.php中添加以下内容

1- 将ServiceProvider添加到providers数组中

DutchCodingCompany\NoCaptcha\NoCaptchaServiceProvider::class,

2- 将类别名添加到aliases数组中

'NoCaptcha' => DutchCodingCompany\NoCaptcha\Facades\NoCaptcha::class,

3- 发布配置文件

php artisan vendor:publish --provider="DutchCodingCompany\NoCaptcha\NoCaptchaServiceProvider"

配置

.env文件中添加NOCAPTCHA_SECRETNOCAPTCHA_SITEKEY

NOCAPTCHA_SECRET=secret-key
NOCAPTCHA_SITEKEY=site-key

(您可以从这里获取它们)

用法

初始化js源

使用默认选项

 {!! NoCaptcha::renderJs() !!}

使用语言支持onloadCallback选项

 {!! NoCaptcha::renderJs('fr', true, 'recaptchaCallback') !!}

显示reCAPTCHA

默认小部件

{!! NoCaptcha::display() !!}

使用自定义属性(主题、大小、回调...)

{!! NoCaptcha::display(['data-theme' => 'dark']) !!}

使用提交按钮的无障碍reCAPTCHA

{!! NoCaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}

注意,在此方法中,表单的id是必需的,以便在成功验证验证码后自动提交表单。

验证

'g-recaptcha-response' => 'required|captcha'添加到规则数组中

$validate = Validator::make(Input::all(), [
	'g-recaptcha-response' => 'required|captcha'
]);
自定义验证消息

将以下值添加到validation语言文件中的custom数组中

'custom' => [
    'g-recaptcha-response' => [
        'required' => 'Please verify that you are not a robot.',
        'captcha' => 'Captcha error! try again later or contact site admin.',
    ],
],

然后在Form中检查验证码错误

@if ($errors->has('g-recaptcha-response'))
    <span class="help-block">
        <strong>{{ $errors->first('g-recaptcha-response') }}</strong>
    </span>
@endif

测试

当使用Laravel测试功能时,您需要模拟验证码表单元素的响应。

因此,对于涉及验证码的任何表单测试,您可以通过模拟外观行为来完成此操作

// prevent validation error on captcha
NoCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// provide hidden input for your 'required' validation
NoCaptcha::shouldReceive('display')
    ->zeroOrMoreTimes()
    ->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');

然后您可以根据正常方式测试表单的其余部分。

当使用HTTP测试时,您可以将g-recaptcha-response添加到请求体中,以进行'必须'验证

// prevent validation error on captcha
NoCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// POST request, with request body including g-recaptcha-response
$response = $this->json('POST', '/register', [
    'g-recaptcha-response' => '1',
    'name' => 'John',
    'email' => 'john@example.com',
    'password' => '123456',
    'password_confirmation' => '123456',
]);

不使用Laravel

请查看下面的示例

<?php

require_once "vendor/autoload.php";

$secret  = 'CAPTCHA-SECRET';
$sitekey = 'CAPTCHA-SITEKEY';
$captcha = new \DutchCodingCompany\NoCaptcha\NoCaptcha($secret, $sitekey);

if (! empty($_POST)) {
    var_dump($captcha->verifyResponse($_POST['g-recaptcha-response']));
    exit();
}

?>

<form action="?" method="POST">
    <?php echo $captcha->display(); ?>
    <button type="submit">Submit</button>
</form>

<?php echo $captcha->renderJs(); ?>

贡献

https://github.com/DutchCodingCompany/no-captcha/pulls