六安应用/recaptcha-bundle

此扩展包为 symfony 提供谷歌 reCAPTCHA

安装: 239

依赖者: 0

建议者: 0

安全: 0

类型:symfony-bundle

1.0.4 2019-04-11 12:43 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:43:34 UTC


README

此扩展包为 Symfony 提供了简单的 reCAPTCHA 表单字段。

安装

步骤 1:使用 composer 启用 Bundle

要使用 composer 安装 SixnappsRecaptchaBundle,只需在终端输入

php composer.phar require excelwebzone/recaptcha-bundle

现在,Composer 将自动下载所有必需的文件,并为您安装。您需要做的就是更新您的 `AppKernel.php` 文件,并注册新的扩展包

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Sixnapps\RecaptchaBundle\SixnappsRecaptchaBundle(),
    // ...
);

步骤 2:配置扩展包

将以下内容添加到您的配置文件中

# app/config/config.yml

sixnapps_recaptcha:
    public_key:  here_is_your_public_key
    private_key: here_is_your_private_key
    # Not needed as "%kernel.default_locale%" is the default value for the locale key
    locale_key:  %kernel.default_locale%

注意:此 Bundle 允许客户端浏览器选择安全的 https 或不安全的 http API。

如果您想使用与请求区域相同的语言默认值,您必须激活解析器(默认未激活)

# app/config/config.yml

sixnapps_recaptcha:
    // ...
    locale_from_request: true

您可以轻松禁用 reCAPTCHA(例如在本地或测试环境中)

# app/config/config.yml

sixnapps_recaptcha:
    // ...
    enabled: false

甚至可以使用 Ajax 加载 reCAPTCHA

# app/config/config.yml

sixnapps_recaptcha:
    // ...
    ajax: true

www.google.com 在中国大陆被封锁,您可以通过这种方式覆盖默认服务器

# app/config/config.yml

sixnapps_recaptcha:
    // ...
    api_host: recaptcha.net

您可以添加 HTTP 代理配置

# app/config/config.yml

sixnapps_recaptcha:
    // ...
    http_proxy:
        host: proxy.mycompany.com
        port: 3128
        auth: proxy_username:proxy_password

如果您已关闭 reCAPTCHA 端的域名检查,您需要通过启用 `verify_host` 选项来检查响应的来源

# app/config/config.yml

sixnapps_recaptcha:
    // ...
    verify_host: true

恭喜!您已准备就绪!

基本用法

在创建新的表单类时,将以下行添加到创建字段

<?php

use Sixnapps\Bundle\RecaptchaBundle\Form\Type\SixnappsRecaptchaType;

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', SixnappsRecaptchaType::class);
    // ...
}

注意:在低于 2.8 的 Symfony 版本中,通过名称而不是类名引用表单类型,使用

<?php

public function buildForm(FormBuilder $builder, array $options)
{
   // ...
   $builder->add('recaptcha', 'sixnapps_recaptcha');
   // ...
}

您可以使用 "attr > options" 选项将额外选项传递给 reCAPTCHA

<?php

use Sixnapps\Bundle\RecaptchaBundle\Form\Type\SixnappsRecaptchaType;

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', SixnappsRecaptchaType::class, array(
        'attr' => array(
            'options' => array(
                'theme' => 'light',
                'type'  => 'image',
                'size'  => 'normal',
                'defer' => true,
                'async' => true,
            )
        )
    ));
    // ...
}

支持 Google 的隐形模式非常简单

<?php

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', SixnappsRecaptchaType::class, array(
        'attr' => array(
            'options' => array(
                'theme' => 'light',
                'type'  => 'image',
                'size' => 'invisible',              // set size to invisible
                'defer' => true,
                'async' => true,
                'callback' => 'onReCaptchaSuccess', // callback will be set by default if not defined (along with JS function that validate the form on success)
                'bind' => 'btn_submit',             // this is the id of the form submit button
                // ...
             )
        )
    ));
    // ...
}

注意:如果您使用预定义的回调,您需要将 recaptcha-form 类添加到您的 <form> 标签。

如果您需要根据您的网站语言(多站语言)配置 captcha 的语言,您可以使用 "language" 选项传递语言

<?php

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', SixnappsRecaptchaType::class, array(
        'language' => 'en'
        // ...
    ));
    // ...
}

要验证字段,使用

<?php

use Sixnapps\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;

/**
 * @Recaptcha\IsTrue
 */
public $recaptcha;

另一种方法是通过将验证约束作为您的 FormType 的选项来传递。这样,您的数据类只包含有意义的属性。如果我们以上面的示例为例,buildForm 方法将如下所示。请注意,如果设置了 `mapped=>false,则注释将不起作用。您必须同时设置constraints`

<?php

use Sixnapps\Bundle\RecaptchaBundle\Form\Type\SixnappsRecaptchaType;
use Sixnapps\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', SixnappsRecaptchaType::class, array(
        'attr'        => array(
            'options' => array(
                'theme' => 'light',
                'type'  => 'image',
                'size'  => 'normal'
            )
        ),
        'mapped'      => false,
        'constraints' => array(
            new RecaptchaTrue()
        )
    ));
    // ...

表单模板资源现在通过容器的扩展自动注册。但是,您始终可以实现您自己的自定义表单小部件。

PHP:

<?php $view['form']->setTheme($form, array('SixnappsRecaptchaBundle:Form')) ?>

<?php echo $view['form']->widget($form['recaptcha'], array(
    'attr' => array(
        'options' => array(
            'theme' => 'light',
            'type'  => 'image',
            'size'  => 'normal'
        ),
    ),
)) ?>

Twig:

{% form_theme form '@SixnappsRecaptcha/Form/sixnapps_recaptcha_widget.html.twig' %}

{{ form_widget(form.recaptcha, { 'attr': {
    'options' : {
        'theme': 'light',
        'type': 'image',
        'size': 'normal'
    },
} }) }}

如果您不使用表单,您仍然可以使用 JavaScript 实现 reCAPTCHA 字段。

PHP:

<div id="recaptcha-container"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.getScript("<?php echo \Sixnapps\Bundle\RecaptchaBundle\Form\Type\SixnappsRecaptchaType::RECAPTCHA_API_JS_SERVER ?>", function() {
            Recaptcha.create("<?php echo $form['recaptcha']->get('public_key') ?>", "recaptcha-container", {
                theme: "clean",
            });
        });
    };
</script>

Twig:

<div id="recaptcha-container"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.getScript("{{ constant('\\Sixnapps\\Bundle\\RecaptchaBundle\\Form\\Type\\SixnappsRecaptchaType::RECAPTCHA_API_JS_SERVER') }}", function() {
            Recaptcha.create("{{ form.recaptcha.get('public_key') }}", "recaptcha-container", {
                theme: "clean"
            });
        });
    });
</script>

自定义

如果您想使用自定义主题,请在设置主题之前放置您的代码块。

 <div id="recaptcha_widget">
   <div id="recaptcha_image"></div>
   <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div>

   <span class="recaptcha_only_if_image">Enter the words above:</span>
   <span class="recaptcha_only_if_audio">Enter the numbers you hear:</span>

   <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />

   <div><a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a></div>
   <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type('audio')">Get an audio CAPTCHA</a></div>
   <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type('image')">Get an image CAPTCHA</a></div>

   <div><a href="javascript:Recaptcha.showhelp()">Help</a></div>
 </div>

{% form_theme form '@SixnappsRecaptcha/Form/sixnapps_recaptcha_widget.html.twig' %}

{{ form_widget(form.recaptcha, { 'attr': {
    'options' : {
        'theme' : 'custom',
    },
} }) }}