everson-marcos / yii2-recaptcha-widget
Yii2 Google reCAPTCHA v2 和 v3 小部件
2.1.2
2023-09-06 00:43 UTC
Requires
- yiisoft/yii2: ~2.0.11
- yiisoft/yii2-httpclient: *
Requires (Dev)
- phpunit/phpunit: ^4.8.34
This package is not auto-updated.
Last update: 2024-09-18 15:22:22 UTC
README
基于 Google reCaptcha API 2.0 和 3.0。
升级到 2.x 版本
警告!类 ReCaptcha
和 ReCaptchaValidator
已过时。请将它们替换为 ReCaptchaConfig
、ReCaptcha2
和 ReCaptchaValidator2
。
安装
安装此扩展的首选方式是通过 composer。
- 运行以下命令之一
php composer.phar require --prefer-dist "himiklab/yii2-recaptcha-widget" "*"
或者
"himiklab/yii2-recaptcha-widget" : "*"
将以下内容添加到应用程序的 composer.json
文件的 require
部分。
-
在配置文件(web.php)中配置组件。参数 siteKey 和 secret 是可选的。但如果你省略了它们,你需要在每个验证规则和每个你想使用此小部件的视图中设置它们。如果单个视图或验证规则中设置了 siteKey 或 secret,则将覆盖配置中的设置。
'components' => [ 'reCaptcha' => [ 'class' => 'himiklab\yii2\recaptcha\ReCaptchaConfig', 'siteKeyV2' => 'your siteKey v2', 'secretV2' => 'your secret key v2', 'siteKeyV3' => 'your siteKey v3', 'secretV3' => 'your secret key v3', ], ...
或使用 DI 容器
'container' => [ 'definitions' => [ himiklab\yii2\recaptcha\ReCaptcha2::className() => function ($container, $params, $config) { return new himiklab\yii2\recaptcha\ReCaptcha2( 'your siteKey v2', '', // default $config ); }, himiklab\yii2\recaptcha\ReCaptchaValidator2::className() => function ($container, $params, $config) { return new himiklab\yii2\recaptcha\ReCaptchaValidator2( 'your secret key v2', '', // default null, // default null, // default $config ); }, ], ],
- 在你的模型中添加
ReCaptchaValidator2
或ReCaptchaValidator3
,例如
v2
public $reCaptcha; public function rules() { return [ // ... [['reCaptcha'], \himiklab\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'], \himiklab\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( \himiklab\yii2\recaptcha\ReCaptcha2::className(), [ 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up ] ) ?>
v3
<?= $form->field($model, 'reCaptcha')->widget( \himiklab\yii2\recaptcha\ReCaptcha3::className(), [ 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up 'action' => 'homepage', ] ) ?>
或者
v2
<?= \himiklab\yii2\recaptcha\ReCaptcha2::widget([ 'name' => 'reCaptcha', 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up 'widgetOptions' => ['class' => 'col-sm-offset-3'], ]) ?>
v3
<?= \himiklab\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验证!