dmishh / recaptcha-bundle
为 Symfony2 提供的 reCAPTCHA 支持,易于使用的安全组件集成。
1.0.1
2014-12-20 09:11 UTC
Requires
- dmishh/recaptcher: 1.0.*
- symfony/form: ~2.1
- symfony/framework-bundle: ~2.1
- symfony/security: ~2.1
- symfony/validator: ~2.1
This package is auto-updated.
Last update: 2024-09-14 07:21:17 UTC
README
特性
- 易于使用
- 面向服务的架构
- 表单集成
- 安全组件集成 — 使用 reCAPTCHA 保护登录表单
- 独立的 reCAPTCHA API 实现(默认为 Recaptcher)
- 支持 PHP 和 Twig 模板引擎
使用 Composer 安装
-
将以下内容添加到您的
composer.json
文件中// composer.json { // ... "require": { // ... "dmishh/recaptcha-bundle": "1.0.*" } }
-
更新依赖项,从命令行运行
php composer.phar update
-
在您的
AppKernel.php
文件中注册捆绑包<?php // in AppKernel::registerBundles() $bundles = array( // ... new Dmishh\Bundle\RecaptchaBundle\RecaptchaBundle() );
配置
您可以在您的 recaptcha 管理页面 找到 reCAPTCHA 的公钥和私钥。在 config.yml
中配置 RecaptchaBundle
# ... recaptcha: public_key: 6LeJg90SAAAAAA9yk0zeNrF8QKaxqR_bV_9SNLz9 private_key: 6LeJg90SAAAAAEuTLEbZuymhkigzzPm2_wsSdA8j use_https: false # optional
在表单中的使用
添加以下行以创建 reCAPTCHA 字段
<?php // your form ... public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add('recaptcha', 'recaptcha'); }
您可以使用 widget_options 选项向 reCAPTCHA 传递额外的选项
<?php // your form ... public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add('recaptcha', 'recaptcha', array( 'widget_options' => array( 'theme' => 'clean' ) )); }
有效选项列表
- theme
- lang
- custom_translations
- custom_theme_widget
- tabindex
请访问 自定义 reCAPTCHA 的外观和感觉 了解自定义的详细信息。
验证
RecaptchaType
内置验证器,您不需要做更多。只需使用它!
但如果你因为某些原因需要 禁用 验证,那么请移除现有的验证器
<?php // your form ... public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add('recaptcha', 'recaptcha', array( // only for disabling validation 'constraints' => array() )); }
在登录表单中的使用
您需要在 services.yml
中定义 RecaptchaFormAuthenticationListener
作为表单身份验证的默认监听器
# ... security.authentication.listener.form: class: Dmishh\Bundle\RecaptchaBundle\Security\Firewall\RecaptchaFormAuthenticationListener parent: security.authentication.listener.abstract calls: - [setRecaptcha, [@recaptcha]] abstract: true
setRecaptcha 方法的第二个参数用于禁用验证器。这可能在你需要在开发环境中禁用检查时很有用。例如
security.authentication.listener.form: class: Dmishh\Bundle\RecaptchaBundle\Security\Firewall\RecaptchaFormAuthenticationListener parent: security.authentication.listener.abstract calls: - [setRecaptcha, [@recaptcha, %kernel.debug%]] abstract: true
现在您需要将 reCAPTCHA 字段添加到您的表单中。如果您正在使用自己的表单类型,则按如下方式将 reCAPTCHA 字段添加到您的表单中。
否则,将以下行添加到渲染登录表单的控制器中以及相应的 Twig 模板中
<?php // your controller ... public function yourAction(Request $request) { // ... $recaptcha = $this->createForm($this->get('form.type.recaptcha')); return array( // ... 'recaptcha' => $recaptcha->createView() ); }
<!-- template --> <label for="recaptcha_response_field">Captcha:</label> {{ form_widget(recaptcha) }}
本文档基于 EWZRecaptchaBundle 的文档。
路线图
1.0.* (待办事项)
- 添加有关手动安装和使用 AJAX 的 reCAPTCHA 的文档
1.0
- 初始版本