salberts/symfony2recaptcha

此扩展包提供了方便的reCAPTCHA表单字段集成

安装: 15

依赖: 0

建议者: 0

安全性: 0

星级: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.4.7 2015-08-01 14:51 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:57:49 UTC


README

此扩展包为Symfony提供了方便的reCAPTCHA表单字段

同时实现了Silex框架的桥梁: 跳转到文档

安装

步骤 1:使用composer并启用Bundle

使用composer安装EWZRecaptchaBundle,只需将以下内容添加到您的composer.json文件中

// composer.json
{
    // ...
    "require": {
        // ...
        "excelwebzone/recaptcha-bundle": "dev-master"
    }
}

注意:请将上述代码片段中的dev-master替换为最新的稳定分支,例如2.3.*

然后,您可以通过从您的composer.json文件所在的目录运行Composer的update命令来安装新的依赖项

$ php composer.phar update

现在,Composer将自动下载所有必要的文件,并为您安装它们。接下来,您只需更新您的AppKernel.php文件,并注册新的Bundle

<?php

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new EWZ\Bundle\RecaptchaBundle\EWZRecaptchaBundle(),
    // ...
);

步骤 2:配置Bundle

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

# app/config/config.yml

ewz_recaptcha:
    public_key:  here_is_your_public_key
    private_key: here_is_your_private_key
    locale_key:  %kernel.default_locale%

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

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

# app/config/config.yml

ewz_recaptcha:
    // ...
    enabled: false

甚至可以使用Ajax加载reCAPTCHA

# app/config/config.yml

ewz_recaptcha:
    // ...
    ajax: true

您还可以添加HTTP代理配置

# app/config/config.yml

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

恭喜!您已准备好!

基本用法

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

<?php

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

您可以使用“attr > options”选项传递额外的reCAPTCHA选项

<?php

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'ewz_recaptcha', array(
        'attr' => array(
            'options' => array(
                'theme' => 'light',
                'type'  => 'image'
            )
        )
    ));
    // ...
}

要验证字段,请使用

<?php

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

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

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

<?php

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;

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

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

PHP:

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

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

Twig:

{% form_theme form 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %}

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

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

PHP:

<div id="recaptcha-container"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.getScript("<?php echo \EWZ\Bundle\RecaptchaBundle\Form\Type\RecaptchaType::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('\\EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\RecaptchaType::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 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %}

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

进一步阅读自定义reCAPTCHA的外观和感觉