gremo / captcha-form-bundle
提供 CAPTCHA 表单字段并支持多种适配器的 Symfony 扩展包。
Requires
- php: >=5.3.3
- symfony/framework-bundle: ^2.4 || ^3.0 || ^4.0
Requires (Dev)
- google/recaptcha: ^1.1
- gregwar/captcha: ^1.1
- symfony/form: ^2.4 || ^3.0|| ^4.0
- symfony/options-resolver: ^2.4 || ^3.0|| ^4.0
- symfony/validator: ^2.4 || ^3.0|| ^4.0
Suggests
- google/recaptcha: Allows you to use Google reCAPTCHA adapter
- gregwar/captcha: Allows you to use Gregwar/Captcha adapter
README
提供 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 ]);