xorock / zend-service-recaptcha-v2
Zend Framework 3 service for Google ReCaptcha v2
Installs: 3 203
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^5.5 || ^7.0
- zendframework/zend-captcha: ^2.6
- zendframework/zend-form: ^2.9
- zendframework/zend-http: ^2.5
- zendframework/zend-json: ^3.0
- zendframework/zend-modulemanager: ^2.7
- zendframework/zend-stdlib: ^3.1
This package is not auto-updated.
Last update: 2025-01-04 21:36:21 UTC
README
#Zend Framework 3 integration with Google ReCaptcha v2
Provides Google ReCaptcha v2 integration for Zend Framework 3.
Installation
Install this library using composer:
$ composer require xorock/zend-service-recaptcha-v2
Then add ZfServiceReCaptcha2
to Your module config under the modules
key.
Using Zend Framework Captcha element
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' ], ], ], ]);
Options
Form element allows two different type of options: params and attributes. Both refer to https://developers.google.com/recaptcha/docs/display configuration options. Parameters are published inside 'script' tag, while the attributes referes to 'div.g-recaptcha element'. By default they are defined as:
/** * 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 form element
ZfServiceReCaptcha2 component also comes with a predefined element ReCaptcha2
, which extends built-in \Zend\Form\Element\Captcha
.
By default, it consumes following config:
return [ 'zfservicerecaptcha2' => [ 'recaptcha' => [ 'options' => [ // Captcha options 'hl' => 'en', 'public_key' => 'Generated public key', 'private_key' => 'Generated private key' ], ] ] ];
It is convenient to set Your ReCaptcha keys and other options in general application configuration. You can use this element by simply defining
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', ], ]);