alikdex/yii2-recaptcha

无需curl的reCaptcha

安装: 51

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 1

类型:yii2-extension

2.0.0 2018-05-21 12:38 UTC

This package is auto-updated.

Last update: 2024-09-17 09:31:12 UTC


README

基于Google reCaptcha API 2.0。

安装

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

  • 运行以下命令:
php composer.phar require --prefer-dist "alikdex/yii2-recaptcha" "2.0.0"

或者

"alikdex/yii2-recaptcha" : "2.0.0"

将以下内容添加到应用程序的 composer.json 文件的 require 部分:

  • 注册reCAPTCHA API密钥.

  • 在配置文件(web.php)中配置组件。siteKey和secret参数是可选的。但如果您省略了它们,您需要在每个验证规则和每个您想要使用此小部件的视图中设置它们。如果某个视图或验证规则中设置了siteKey或secret,它将覆盖配置中设置的内容。

'components' => [
    'container' => [
        'definitions' => [
            Adx\ReCaptcha\ReCaptcha::class => [
                'siteKey' => 'Your site key',
            ],
            Adx\ReCaptcha\ReCaptchaValidator::class => [
                'secret' => 'Your secret key',
            ],
        ],
    ],
    ...
],
  • 在您的模型中添加 ReCaptchaValidator,例如
public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['captcha'], \Adx\ReCaptcha\ReCaptchaValidator::class],
  ];
}

或者直接

public function rules()
{
  return [
      // ...
      [[], \Adx\ReCaptcha\ReCaptchaValidator::class],
  ];
}

使用方法

例如

<?= \Adx\ReCaptcha\ReCaptcha::widget([
    'name' => 'captcha', // optional
    'widgetOptions' => [
      'class' => 'col-sm-offset-3',
      'data-theme' => 'dark',
      // ...  see google recaptcha2 manual
    ]
]) ?>

或者

<?= $form->field($model, 'captcha')->widget(\Adx\ReCaptcha\ReCaptcha::class) ?>

或者简单

<?= \Adx\ReCaptcha\ReCaptcha::widget() ?>

资源