jield-webdev/laminas-recaptcha

Laminas 模块,让您轻松集成谷歌的新、更简单的 recaptcha(我不是机器人)

v2.0 2022-07-18 12:58 UTC

README

此仓库是 https://github.com/Saeven/zf2-circlical-recaptcha 的分支,并升级以支持 Laminas。此库仅支持谷歌 ReCaptcha v3,若要支持谷歌 ReCaptcha v2,请使用 主仓库

实现

系统使用 form_elements 工厂,因此 captcha 表单元素不能在表单构造函数中直接调用,而必须通过 init() 调用

以下是一个使用 captcha 表单元素的注册表单示例

通过 composer 安装模块

composer require jield-webdev/laminas-recaptcha

修改 circlical.recaptcha.local.php,并将值更改为在 ReCaptcha 控制台 上可以找到的值。确保您注册了一个 v3 应用程序

<?php

declare(strict_types=1);

namespace Admin\Form\User;

use CirclicalRecaptcha\Form\Element\Recaptcha;
use Contact\Entity\OptIn;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use DoctrineORMModule\Form\Element\EntityMultiCheckbox;
use Laminas\Form\Element\Csrf;
use Laminas\Form\Element\Email;
use Laminas\Form\Element\Text;
use Laminas\Form\Form;

use function _;
use function sprintf;

final class Register extends Form
{
    public function __construct(private readonly EntityManager $entityManager)
    {
        parent::__construct();
    }

    public function init(): void
    {
        $this->setAttribute('action', '');

        $this->add(
            [
                'name' => 'firstName',
                'type' => Text::class,
                'options' => [
                    'label' => _('txt-first-name'),
                ],
                'attributes' => [
                    'placeholder' => _('txt-give-your-first-name'),
                ],
            ]
        );
        $this->add(
            [
                'name' => 'lastName',
                'type' => Text::class,
                'options' => [
                    'label' => _('txt-last-name'),
                ],
                'attributes' => [
                    'placeholder' => _('txt-give-your-last-name'),
                ],
            ]
        );
        $this->add(
            [
                'name' => 'email',
                'type' => Email::class,
                'options' => [
                    'label' => _('txt-company-email-address'),
                ],
                'attributes' => [
                    'placeholder' => _('txt-give-your-company-email-address'),
                ],
            ]
        );       
        $this->add(
            [
                'name' => 'g-recaptcha-response',
                'type' => Recaptcha::class,
            ]
        );
        $this->add(
            [
                'name' => 'csrf',
                'type' => Csrf::class,
            ]
        );
        $this->add(
            [
                'name' => 'submit',
                'type' => 'submit',
                'attributes' => [
                    'class' => 'btn btn-primary',
                    'value' => _('txt-register'),
                ],
            ]
        );
    }
}

module.config.php 中注册表单,使用 ConfigAbstractFactory 模式注册更多服务或在不需要依赖项时使用可调用对象

'form_elements' => [
    'factories' => [
        Register::class => ConfigAbstractFactory::class,
    ],
],

表单必须通过 FormElementManager 注入到 Controller 中,当使用 ConfigAbstractFactory 系统时,可以这样操作

UserController::class => [
   'FormElementManager'
],

在控制器中,表单可以按以下方式调用

//To properly load the captcha, we need to use the formElementManager to get the form
$form = $this->formElementManager->get(Register::class);