carlos-mg89 / symfony-captcha-bundle
此包的最新版本(4.2.21)没有可用的许可证信息。
Symfony Captcha Bundle -- 为 Symfony 框架集成的 BotDetect PHP CAPTCHA 生成器。
4.2.21
2020-12-09 16:08 UTC
Requires
- php: >=5.3.9
- captcha-com/captcha: 4.*
This package is auto-updated.
Last update: 2024-09-09 00:52:30 UTC
README
BotDetect PHP Captcha 生成器与 Symfony 5 框架的集成(可能也适用于 Symfony 4.4)
captcha.com 上的 BotDetect Symfony CAPTCHA 集成
- Symfony Captcha 集成快速入门
- Symfony 应用程序
- Symfony Captcha API 基础示例
- Symfony Captcha 表单模型验证示例
- Symfony Captcha FOSUserBundle 示例
其他 BotDetect PHP Captcha 集成
有疑问?
如果您遇到错误、实施问题、想要讨论的使用场景,或者有任何问题,请联系 BotDetect CAPTCHA 支持。
如何使用 Composer 安装
只需运行 composer require carlos-mg89/symfony-captcha-bundle
在 Symfony 5.x 项目中的使用示例
- 如上所述使用 Composer 安装依赖项
- 在您的
config/routes.yaml
中添加以下内容captcha_routing: resource: "@CaptchaBundle/Resources/config/routing.yml"
- 创建此文件
config/packages/captcha.php
,内容如下(或类似)<?php if (!class_exists('CaptchaConfiguration')) { return; } // You could have more than one object like ExampleCaptcha. For example, one for the login page, another for the register page, etc. return [ 'ExampleCaptcha' => [ 'UserInputID' => 'captchaCode', 'CodeLength' => CaptchaRandomization::GetRandomCodeLength(5, 6), 'ImageWidth' => 250, 'ImageHeight' => 50, ], ];
- 编辑您的
config/services.yaml
,以便自动连接库中使用的控制器# We need to autowire the Container (or manually wire it) services: Captcha\Bundle\CaptchaBundle\Controller\: resource: '../vendor/carlos-mg89/symfony-captcha-bundle/Controller' autowire: true
- 编辑您的
FormType
或FormBuilderInterface
,添加以下内容以添加 captcha 并验证表单约束$builder->add('captchaCode', CaptchaType::class, [ 'captchaConfig' => 'ExampleCaptcha', 'constraints' => [ new ValidCaptcha([ 'message' => 'Invalid captcha, please try again', ]), ] ]);
- 现在编辑您的 Twig 模板,添加新的
captchaCode
(CaptchaType
){{ form_label(form.captchaCode) }} {{ form_widget(form.captchaCode}) }}
- 最后,添加表单验证
$contactForm = $this->createForm(ContactType::class); $contactForm->handleRequest($request); if ($contactForm->isSubmitted() && $contactForm->isValid()) { // Do whatever you want, be it register a user, send an email, etc $this->addFlash('success', $translator->trans('Contact.Form.SuccessMsg')); } elseif ($contactForm->isSubmitted() && !$contactForm->isValid()) { throw new Exception('Invalid form'); }
您可以在以下位置找到原始文档: https://captcha.com/doc/php/howto/symfony-captcha-bundle-integration.html#howto_display_captcha_config,但它不是完全最新的,也不是完整的。