m1roff / yii2-recaptcha-widget
Yii2 Google reCAPTCHA v2 和 v3 小部件
1.0.1
2022-09-08 20:17 UTC
Requires
- php: ^8.0
- yiisoft/yii2: ^2.0.20
- yiisoft/yii2-httpclient: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.10
This package is auto-updated.
Last update: 2024-09-09 01:06:32 UTC
README
基于 Google reCaptcha API 2.0 和 3.0。
是从 https://packagist.org.cn/packages/himiklab/yii2-recaptcha-widget 分支出来的版本。
安装
安装此扩展的首选方式是通过 composer。
- 可以运行
composer require --prefer-dist "m1roff/yii2-recaptcha-widget" "^1.0"
-
在配置文件(web.php)中配置组件。siteKey 和 secret 参数是可选的。但是,如果您省略了它们,您需要在每个验证规则和每个您想使用此小部件的视图中设置它们。如果单独的视图或验证规则中设置了 siteKey 或 secret,则将覆盖配置中的设置。
'components' => [ ReCaptchaConfig::COMPONENT_ID => [ 'class' => ReCaptchaConfig::class, 'siteKeyV2' => 'your siteKey v2', 'secretV2' => 'your secret key v2', 'siteKeyV3' => 'your siteKey v3', 'secretV3' => 'your secret key v3', ], ...
或者使用 DI 容器
'container' => [ 'definitions' => [ m1roff\yii2\recaptcha\ReCaptcha2::class => function ($container, $params, $config) { return new m1roff\yii2\recaptcha\ReCaptcha2( 'your siteKey v2', '', // default $config ); }, m1roff\yii2\recaptcha\ReCaptchaValidator2::class => function ($container, $params, $config) { return new m1roff\yii2\recaptcha\ReCaptchaValidator2( 'your secret key v2', '', // default null, // default null, // default $config ); }, ], ],
- 在您的模型中添加
ReCaptchaValidator2
或ReCaptchaValidator3
,例如
v2
public $reCaptcha; public function rules() { return [ // ... [['reCaptcha'], \m1roff\yii2\recaptcha\ReCaptchaValidator2::class, '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'], \m1roff\yii2\recaptcha\ReCaptchaValidator3::class, 'secret' => 'your secret key', // unnecessary if reСaptcha is already configured 'threshold' => 0.5, 'action' => 'homepage', ], ]; }
使用方法
例如
v2
<?= $form->field($model, 'reCaptcha')->widget( \m1roff\yii2\recaptcha\ReCaptcha2::class, [ 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up ] ) ?>
v3
<?= $form->field($model, 'reCaptcha')->widget( \m1roff\yii2\recaptcha\ReCaptcha3::class, [ 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up 'action' => 'homepage', ] ) ?>
或者
v2
<?= \m1roff\yii2\recaptcha\ReCaptcha2::widget([ 'name' => 'reCaptcha', 'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up 'widgetOptions' => ['class' => 'col-sm-offset-3'], ]) ?>
v3
<?= \m1roff\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 验证!