carlos-mg89/symfony-captcha-bundle

此包的最新版本(4.2.21)没有可用的许可证信息。

Symfony Captcha Bundle -- 为 Symfony 框架集成的 BotDetect PHP CAPTCHA 生成器。

安装: 15,433

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 17

类型:symfony-bundle

4.2.21 2020-12-09 16:08 UTC

README

BotDetect PHP Captcha 生成器与 Symfony 5 框架的集成(可能也适用于 Symfony 4.4)

Total Downloads Latest Stable Version

BotDetect PHP CAPTCHA Library

captcha.com 上的 BotDetect Symfony CAPTCHA 集成

其他 BotDetect PHP Captcha 集成

有疑问?

如果您遇到错误、实施问题、想要讨论的使用场景,或者有任何问题,请联系 BotDetect CAPTCHA 支持

如何使用 Composer 安装

只需运行 composer require carlos-mg89/symfony-captcha-bundle

在 Symfony 5.x 项目中的使用示例

  1. 如上所述使用 Composer 安装依赖项
  2. 在您的 config/routes.yaml 中添加以下内容
    captcha_routing:
          resource: "@CaptchaBundle/Resources/config/routing.yml"
    
  3. 创建此文件 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,
       ],
     ];
    
  4. 编辑您的 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
    
  5. 编辑您的 FormTypeFormBuilderInterface,添加以下内容以添加 captcha 并验证表单约束
    $builder->add('captchaCode', CaptchaType::class, [
        'captchaConfig' => 'ExampleCaptcha',
        'constraints' => [
            new ValidCaptcha([
                'message' => 'Invalid captcha, please try again',
            ]),
        ]
    ]);
    
  6. 现在编辑您的 Twig 模板,添加新的 captchaCodeCaptchaType
    {{ form_label(form.captchaCode) }}
    {{ form_widget(form.captchaCode}) }}
    
  7. 最后,添加表单验证
    $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,但它不是完全最新的,也不是完整的。