kekaadrenalin/yii2-recaptcha-widget

Yii2 Google reCAPTCHA v2 和 v3 小部件

安装: 808

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 106

开放问题: 0

类型:yii2-extension

2.1.2 2020-11-06 10:13 UTC

This package is auto-updated.

Last update: 2024-09-06 19:50:32 UTC


README

基于 Google reCaptcha API 2.0 和 3.0。

Packagist Packagist license

升级到 2.x 版本

警告!类 ReCaptchaReCaptchaValidator 已弃用。请将它们替换为 ReCaptchaConfigReCaptcha2ReCaptchaValidator2

安装

通过 composer 安装此扩展是首选方式。

  • 运行以下命令:
php composer.phar require --prefer-dist "kekaadrenalin/yii2-recaptcha-widget" "*"

或者

"kekaadrenalin/yii2-recaptcha-widget" : "*"

将以下内容添加到应用程序的 composer.json 文件的 require 部分:

  • 注册 reCAPTCHA API 密钥.

  • 在配置文件(web.php)中配置组件。siteKey 和 secret 参数是可选的。但如果您省略它们,您需要在每个验证规则和每个想要使用此小部件的视图中设置它们。如果单独的视图或验证规则中设置了 siteKey 或 secret,则将覆盖配置中的设置。

'components' => [
    'reCaptcha' => [
        'class' => 'kekaadrenalin\yii2\recaptcha\ReCaptchaConfig',
        'siteKeyV2' => 'your siteKey v2',
        'secretV2' => 'your secret key v2',
        'siteKeyV3' => 'your siteKey v3',
        'secretV3' => 'your secret key v3',
    ],
    ...

或者使用 DI 容器

'container' => [
    'definitions' => [
        kekaadrenalin\yii2\recaptcha\ReCaptcha2::className() => function ($container, $params, $config) {
            return new kekaadrenalin\yii2\recaptcha\ReCaptcha2(
                'your siteKey v2',
                '', // default
                $config
            );
        },
        kekaadrenalin\yii2\recaptcha\ReCaptchaValidator2::className() => function ($container, $params, $config) {
            return new kekaadrenalin\yii2\recaptcha\ReCaptchaValidator2(
                'your secret key v2',
                '', // default
                null, // default
                null, // default
                $config
            );
        },
    ],
],
  • 在您的模型中添加 ReCaptchaValidator2ReCaptchaValidator3,例如

v2

public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['reCaptcha'], \kekaadrenalin\yii2\recaptcha\ReCaptchaValidator2::className(),
        'secret' => 'your secret key', // unnecessary if reСaptcha is already configured
        'uncheckedMessage' => 'Please confirm that you are not a bot.'],
  ];
}

v3

public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['reCaptcha'], \kekaadrenalin\yii2\recaptcha\ReCaptchaValidator3::className(),
        'secret' => 'your secret key', // unnecessary if reСaptcha is already configured
        'threshold' => 0.5,
        'action' => 'homepage',
      ],
  ];
}

使用方法

例如

v2

<?= $form->field($model, 'reCaptcha')->widget(
    \kekaadrenalin\yii2\recaptcha\ReCaptcha2::className(),
    [
        'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
    ]
) ?>

v3

<?= $form->field($model, 'reCaptcha')->widget(
    \kekaadrenalin\yii2\recaptcha\ReCaptcha3::className(),
    [
        'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
        'action' => 'homepage',
    ]
) ?>

或者

v2

<?= \kekaadrenalin\yii2\recaptcha\ReCaptcha2::widget([
    'name' => 'reCaptcha',
    'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]) ?>

v3

<?= \kekaadrenalin\yii2\recaptcha\ReCaptcha3::widget([
    'name' => 'reCaptcha',
    'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
    'action' => 'homepage',
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]) ?>
  • 注意:请禁用 ReCaptcha 字段的ajax验证!

资源