lethak/excelwebzonerecaptcha-bundle

此包已被弃用,不再维护。作者建议使用excelwebzone/recaptcha-bundle包。

此包提供方便的reCAPTCHA表单字段集成。

安装: 32

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 150

类型:symfony-bundle

v1.4.4 2016-09-13 16:29 UTC

README

此包为Symfony提供易于集成的reCAPTCHA表单字段。

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

安装

步骤1:使用Composer启用Bundle

要在终端安装EWZRecaptchaBundle,只需输入以下命令:

php composer.phar require excelwebzone/recaptcha-bundle

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

<?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
    # Not needed as "%kernel.default_locale%" is the default value for the locale key
    locale_key:  %kernel.default_locale%

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

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

# app/config/config.yml

ewz_recaptcha:
    // ...
    locale_from_request: true

您可以轻松禁用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

您可以选择记住如果验证码已经验证,则不再需要它一段时间(默认:0)。

# app/config/config.yml

ewz_recaptcha:
    // ...
    remember_max_count: 5

恭喜!您已准备好!

基本用法

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

<?php

use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;

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

您可以通过“attr > options”选项传递额外的reCAPTCHA选项。

<?php

use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;

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

如果您需要根据您的站点语言(多站点语言)配置验证码的语言,您可以通过“language”选项传递语言。

<?php

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

要验证字段,请使用

<?php

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

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

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

<?php

use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', EWZRecaptchaType::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('EWZRecaptchaBundle:Form')) ?>

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

Twig:

{% form_theme form 'EWZRecaptchaBundle:Form:ewz_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 \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::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\\EWZRecaptchaType::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',
    },
} }) }}