s4studio / yii2-recaptcha-widget
Yii2 Google reCAPTCHA v2 和 v3 小部件
2.2.0
2022-07-29 07:13 UTC
Requires
- yiisoft/yii2: ~2.0.11
- yiisoft/yii2-httpclient: *
Requires (Dev)
- phpunit/phpunit: ^4.8.34
README
基于 Google reCaptcha API 2.0 和 3.0。
紧急
警告!已移除类 ReCaptcha
和 ReCaptchaValidator
。原项目由 himiklab
提供,已弃用。
安装
安装此扩展的首选方法是通过 composer。
- 运行以下命令
php composer.phar require --prefer-dist "s4studio/yii2-recaptcha-widget" "dev-master"
或
"s4studio/yii2-recaptcha-widget" : "dev-master"
将以下内容添加到您的应用程序的 composer.json
文件的 require
部分。
-
在您的配置文件(web.php)中配置组件。siteKey 和 secret 参数是可选的。但如果你省略了它们,你需要在每个验证规则和每个你希望使用此小部件的视图中设置它们。如果单独的视图或验证规则中设置了 siteKey 或 secret,将覆盖配置中的设置。
'components' => [ 'reCaptcha' => [ 'class' => 's4studio\recaptcha\ReCaptchaConfig', 'siteKeyV2' => 'your siteKey v2', 'secretV2' => 'your secret key v2', 'siteKeyV3' => 'your siteKey v3', 'secretV3' => 'your secret key v3', ], ...
或使用 DI 容器
'container' => [ 'definitions' => [ s4studio\recaptcha\ReCaptcha2::className() => function ($container, $params, $config) { return new s4studio\recaptcha\ReCaptcha2( 'your siteKey v2', '', // default $config ); }, s4studio\recaptcha\ReCaptchaValidator2::className() => function ($container, $params, $config) { return new s4studio\recaptcha\ReCaptchaValidator2( 'your secret key v2', '', // default null, // default null, // default $config ); }, ], ],
- 在您的模型中添加
ReCaptchaValidator2
或ReCaptchaValidator3
,例如
v2
public $reCaptcha; public function rules() { return [ // ... [['reCaptcha'], \s4studio\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'], \s4studio\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( \s4studio\recaptcha\ReCaptcha2::className(), [ 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up ] ) ?>
v3
<?= $form->field($model, 'reCaptcha')->widget( \s4studio\recaptcha\ReCaptcha3::className(), [ 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up 'action' => 'homepage', ] ) ?>
或
v2
<?= \s4studio\recaptcha\ReCaptcha2::widget([ 'name' => 'reCaptcha', 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up 'widgetOptions' => ['class' => 'col-sm-offset-3'], ]) ?>
v3
<?= \s4studio\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 验证!