jield-webdev/laminas-recaptcha 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jield-webdev/laminas-recaptcha

Composer 安装命令:

composer require jield-webdev/laminas-recaptcha

包简介

Laminas module that lets you easily incorporate Google's new, simpler recaptcha (I am not a robot)

README 文档

README

This repo is a fork of https://github.com/Saeven/zf2-circlical-recaptcha and upgraded to support Laminas This library only supports Google ReCaptcha v3, for support of Google ReCaptcha v2 please use the Main repo

Implementation

The system uses form_elements factories so the captcha form element cannot be directly called in the form constructor, but have to be called via an init() call

Here is an example of a registration form using captcha form element

Install the module via composer:

composer require jield-webdev/laminas-recaptcha

And modify circlical.recaptcha.local.php and change the values with the values which can be found on the ReCaptcha dashboard. Make sure you register a v3 application

<?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'),
                ],
            ]
        );
    }
}

Register the form in the module.config.php, use a ConfigAbstractFactory pattern to register more services in the form or use an invokable when no dependencies are needed

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

The form has to be injected in the Controller via the FormElementManager, when using the ConfigAbstractFactory system this can be done as follows:

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

In the controller the form can be called in the following way

//To properly load the captcha, we need to use the formElementManager to get the form
$form = $this->formElementManager->get(Register::class);
docker compose run --rm php vendor/bin/phpstan --memory-limit=2G

统计信息

  • 总下载量: 13.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固