gremo/captcha-form-bundle

提供 CAPTCHA 表单字段并支持多种适配器的 Symfony 扩展包。

安装次数: 9,701

依赖者: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 2

开放问题: 0

类型:symfony-bundle

v1.1.1 2021-11-26 12:19 UTC

This package is auto-updated.

Last update: 2024-08-26 17:57:40 UTC


README

Latest stable Downloads total

提供 CAPTCHA 表单字段以解决挑战-响应测试的 Symfony 扩展包。支持多种适配器,包括自定义适配器。内置适配器包括

欢迎新贡献者!

安装

composer require gremo/captcha-form-bundle

然后启用该扩展包

<?php
// config/bundles.php

return [
    // ...
    Gremo\CaptchaFormBundle\GremoCaptchaFormBundle::class => ['all' => true],
];

如果您使用的是 Symfony 的早期版本

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Gremo\CaptchaFormBundle\GremoCaptchaFormBundle(),
    );
}

配置

gremo_captcha_form:
    # Default template, change only if you know what your are doing
    template: 'GremoCaptchaFormBundle::default.html.twig'

    # Default adapter (default to the first adapter)
    default_adapter: ~

    # Adapters (one or more) configuration
    adapters:
        # Adapter key and its options
        adapter_key1: []

        # ... and another adapter
        adapter_key2: []

为了使用 CAPTCHA 表单,您需要至少配置一个适配器(见“适配器”部分)。

用法

您可以使用通用表单类型而不是每个适配器提供的表单。这更易于维护,因为您只依赖于一个表单类型。

通用类型使用配置中提供的默认适配器和选项。一个示例用法

// For Symfony >= 2.8 and PHP >= 5.5 use the class name resolution via ::class
use Gremo\CaptchaFormBundle\Form\Type\CaptchaType;

$builder->add('captcha', CaptchaType::class, [
    // Pass custom options to override defaults from configuration
]);

// For Symfony >= 2.8 and PHP < 5.5 use the fully-qualified class name as string
$builder->add('captcha', 'Gremo\CaptchaFormBundle\Form\Type\CaptchaType', [
    // Pass custom options to override defaults from configuration
]);

// For Symfony < 2.8
$builder->add('captcha', 'gremo_captcha', [
    // Pass custom options to override defaults from configuration
]);

适配器

必须配置至少一个适配器。

Google reCAPTCHA v2 适配器

适配器密钥: recaptcha 表单类型: Gremo\CaptchaFormBundle\Form\Type\RecaptchaType

google/recaptcha 库添加到您的项目中

composer require google/recaptcha^1

配置适配器(选项说明

# ...
adapters:
    # ...
    recaptcha:
        # Mandatory options
        key:              ~ # string
        secret:           ~ # string

        # Not mandatory options
        theme:            ~ # string
        type:             ~ # string
        size:             ~ # string
        tabindex:         ~ # integer
        callback:         ~ # string
        expired_callback: ~ # string

最后,将 reCAPTCHA 的 <script> 标签添加到您的基模板中

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

提示:将 hl 参数添加到脚本中,以本地化 CAPTCHA,例如在 Twig 中 hl={{ app.request.locale }}

示例用法

// For Symfony >= 2.8 and PHP >= 5.5 use the class name resolution via ::class
use Gremo\CaptchaFormBundle\Form\Type\RecaptchaType;

$builder->add('captcha', RecaptchaType::class, [
    // Pass custom options to override defaults from configuration
]);

// For Symfony >= 2.8 and PHP < 5.5 use the fully-qualified class name as string
$builder->add('captcha', 'Gremo\CaptchaFormBundle\Form\Type\RecaptchaType', [
    // Pass custom options to override defaults from configuration
]);

// For Symfony < 2.8
$builder->add('captcha', 'gremo_captcha_recaptcha', [
    // Pass custom options to override defaults from configuration
]);

Google reCAPTCHA v3 适配器

适配器密钥: recaptcha_v3 表单类型: Gremo\CaptchaFormBundle\Form\Type\RecaptchaV3Type

google/recaptcha 库添加到您的项目中

composer require google/recaptcha^1

配置适配器(选项说明

# ...
adapters:
    # ...
    recaptcha_v3:
        # Mandatory options
        key:              ~ # string
        secret:           ~ # string

        # Not mandatory options
        score_threshold:  ~ # float

不需要添加任何 <script> 标签,因为表单主题会为您处理。

示例用法

// For Symfony >= 2.8 and PHP >= 5.5 use the class name resolution via ::class
use Gremo\CaptchaFormBundle\Form\Type\RecaptchaV3Type;

$builder->add('captcha', RecaptchaV3Type::class, [
    // For options
]);

// For Symfony >= 2.8 and PHP < 5.5 use the fully-qualified class name as string
$builder->add('captcha', 'Gremo\CaptchaFormBundle\Form\Type\RecaptchaV3Type', [
    // For options
]);

// For Symfony < 2.8
$builder->add('captcha', 'gremo_captcha_recaptcha_v3', [
    // For options
]);

Gregwar captcha 适配器

适配器密钥: gregwar_captcha 表单类型: Gremo\CaptchaFormBundle\Form\Type\GregwarCaptchaType

gregwar/recaptcha 库添加到您的项目中

composer require gregwar/recaptcha^1

配置适配器(选项说明

# ...
adapters:
    # ...
    gregwar_captcha:
        # Not mandatory options
        storage_key:        _gregwar_captcha
        width:              ~ # integer
        height:             ~ # integer
        quality:            ~ # integer
        font:               ~ # string
        distorsion:         ~ # boolean
        interpolation:      ~ # boolean
        ignore_all_effects: ~ # boolean
        orc:                ~ # boolean

示例用法

// For Symfony >= 2.8 and PHP >= 5.5 use the class name resolution via ::class
use Gremo\CaptchaFormBundle\Form\Type\GregwarCaptchaType;

$builder->add('captcha', GregwarCaptchaType::class, [
    // Pass custom options to override defaults from configuration
]);

// For Symfony >= 2.8 and PHP < 5.5 use the fully-qualified class name as string
$builder->add('captcha', 'Gremo\CaptchaFormBundle\Form\Type\GregwarCaptchaType', [
    // Pass custom options to override defaults from configuration
]);

// For Symfony < 2.8
$builder->add('captcha', 'gremo_captcha_gregwar', [
    // Pass custom options to override defaults from configuration
]);

Honeypot 适配器

适配器密钥: honeypot 表单类型: Gremo\CaptchaFormBundle\Form\Type\HoneypotType

配置适配器

# ...
adapters:
    # ...
    honeypot:
        # Mandatory options
        type: ~ # string, "text" or "hidden" or their FQCN (Symfony >= 2.8)

示例用法

// For Symfony >= 2.8 and PHP >= 5.5 use the class name resolution via ::class
use Gremo\CaptchaFormBundle\Form\Type\HoneypotType;

$builder->add('captcha', HoneypotType::class, [
    // Pass custom options to override defaults from configuration
]);

// For Symfony >= 2.8 and PHP < 5.5 use the fully-qualified class name as string
$builder->add('captcha', 'Gremo\CaptchaFormBundle\Form\Type\HoneypotType', [
    // Pass custom options to override defaults from configuration
]);

// For Symfony < 2.8
$builder->add('captcha', 'gremo_captcha_honeypot', [
    // Pass custom options to override defaults from configuration
]);