擦除/yii2-recaptcha

安装: 2

依赖项: 0

建议者: 0

安全性: 0

星星: 1

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

dev-master 2020-01-20 09:20 UTC

This package is auto-updated.

Last update: 2024-09-20 19:50:37 UTC


README

为Google ReCAPTCHA v3提供的Yii2小部件和模型行为。

此小部件和行为使您能够轻松地将Google的reCAPTCHA v3(隐形reCAPTCHA)添加到您的项目中。

安装

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

运行以下命令之一:

$ php composer.phar require wiperawa/yii2-recaptcha "dev-master"

或将其添加到您的composer.json文件的require部分:

"wiperawa/yii2-recaptcha": "dev-master"

to the require section of your composer.json file.

用法

  1. 首先,我们需要在google reCAPTCHA 控制台中添加站点并获取SECRET和SITE密钥。

  2. 然后我们必须配置我们使用的模型的行为

use wiperawa\recaptcha\GoogleRecaptchaBehavior;
...
class MyCoolModel extends ActiveRecord {
...
public function behaviors()
    {
        return [
            'googleRecaptchaBehavior' => [
                'class' =>GoogleRecaptchaBehavior::class,
                'secretKey' => 'Google reCAPTCHA Secret Key here',
                'enabled' => true, //Can omit this parameter. just in case you want to temporarily switch off recaptcha 
                'expectedAction' => 'form_submit' //google reCAPTCHA expected action. action we expect from our form.
                'scoreThreshold' => 0.5 //reCAPTCHA score treshold. Optional. default - 0.5
            ],
        ];
    }
...
}
  1. 现在,当我们的模型附加了行为后,我们需要将小部件添加到ActiveForm
use wiperawa\recaptcha\GoogleRecaptchaWidget;
...
<?php $form = ActiveForm::begin(...) ?>

<?= $form->field($model,'recaptchaToken')->
    widget(GoogleRecaptchaWidget::class,[
        'siteKey' => 'Google reCAPTCHA SITEKEY here',
        'expectedAction' => 'form_submit',
        'class' => 'w_google_recaptcha_widget' //Classname of input. optional
    ])?>

这就完成了!小部件将查找'beforeSubmit'动作,然后接收google reCAPTCHA令牌,并提交表单。表单提交后,行为将附加令牌验证,并验证输入。如果发生错误,验证器将错误附加到模型的$ errors属性。