himiklab / yii2-recaptcha-widget
此包已被弃用且不再维护。未建议替代包。
Yii2 Google reCAPTCHA v2 和 v3 小部件
2.1.1
2019-05-14 07:40 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。
升级到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验证!