xorock / zend-service-recaptcha-v2

为 Google ReCaptcha v2 提供的 Zend Framework 3 服务

0.1.0 2016-11-05 07:58 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:04:05 UTC


README

#Zend Framework 3 集成 Google ReCaptcha v2

提供 Google ReCaptcha v2 集成,适用于 Zend Framework 3

安装

使用 composer 安装此库

$ composer require xorock/zend-service-recaptcha-v2

然后在模块配置的 modules 键下添加 ZfServiceReCaptcha2

使用 Zend Framework Captcha 元素

use Zend\Form\Element\Captcha;
use ZfServiceReCaptcha2\Captcha\ReCaptcha2;

$this->add([
    'type'       => Captcha::class,
    'name'       => 'g-recaptcha-response', // name is required for element to be validated
    'options'    => [
        'label' => 'Please answer question',
        'captcha' => [
            'class' => ReCaptcha2::class,
            'options' => [
                'hl' => 'en', // english is set by deafult, this line is not required
                'theme' => 'light', // see options below
                'callback' => '', // callback function, etc.
                'public_key'  => 'Generated public key',
                'private_key' => 'Generated private key'
            ],
        ],
    ],
]);

选项

表单元素允许两种不同的选项:参数和属性。两者都指向 https://developers.google.com/recaptcha/docs/display 配置选项。参数在 'script' 标签内发布,而属性则指向 'div.g-recaptcha 元素'。默认定义如下

/**
 * Parameters for the script object
 *
 * @var array
 */
protected $params = array(
    'onload' => null,
    'render' => 'onload',
    'hl'     => 'en'
);
    
/**
 * Attributes for div element
 *
 * @var array
 */
protected $attributes = array(
    'class'            => 'g-recaptcha',
    'theme'            => 'light',
    'type'             => 'image',
    'tabindex'         => 0,
    'callback'         => null,
    'expired-callback' => null
);

ReCaptcha2 表单元素

ZfServiceReCaptcha2 组件还包含一个预定义的元素 ReCaptcha2,它扩展了内置的 \Zend\Form\Element\Captcha。默认情况下,它使用以下配置

return [
    'zfservicerecaptcha2' => [
        'recaptcha' => [
            'options' => [
                // Captcha options
                'hl' => 'en',
                'public_key'  => 'Generated public key',
                'private_key' => 'Generated private key'
            ],
        ]
    ]
];

在一般应用程序配置中设置您的 ReCaptcha 密钥和其他选项很方便。您可以通过简单地定义以下内容来使用此元素

use ZfServiceReCaptcha2\Form\Element\ReCaptcha2;

$this->add([
    'type'       => ReCaptcha2::class,
    // Field name is defined by factory
    // 'name'       => 'g-recaptcha-response', 
    'options'    => [
        'label' => 'Please answer question',
    ],
]);