mael/recaptcha-bundle

为Symfony和Twig提供Google Recaptcha集成(易于与Symfony表单集成)

1.0.0 2020-05-23 19:56 UTC

This package is auto-updated.

Last update: 2024-09-13 00:57:45 UTC


README

MaelRecaptchaBundle是一个允许在Symfony项目中集成Google Recaptcha的包。

可用功能

  • Google ReCaptcha V2(不可见): ✅
  • Google ReCaptcha V2(复选框): ✅
  • Google ReCaptcha V3: ❌

安装

安装非常快且简单,只需5步

  1. 使用composer安装MaelRecaptchaBundle
  2. 启用该包
  3. 生成你的密钥和密钥
  4. 配置你的密钥和密钥
  5. 在你的表单中使用Recaptcha
  6. 贡献
  7. 许可

步骤1:通过composer安装MaelRecaptchaBundle

运行以下命令

composer require mael/recaptcha-bundle

您可以使用symfony/flex快速配置此包

  • 对于google/recpatcha回答
  • 对于mael/recaptcha-bundle回答

步骤2:启用该包

将包注册到config/bundles.php

<?php

return [
    Mael\MaelRecaptchaBundle\MaelRecaptchaBundle::class => ['all' => true],
];

步骤3:生成你的密钥和密钥

访问以下链接: http://www.google.com/recaptcha/admin

步骤4:配置你的密钥和密钥

在你的.env文件中

YOUR_RECAPTCHA_KEY替换为你的公开密钥,将YOUR_RECAPTCHA_SECRET替换为你的私钥

MAEL_RECAPTCHA_KEY=YOUR_RECAPTCHA_KEY
MAEL_RECAPTCHA_SECRET=YOUR_RECAPTCHA_SECRET

步骤5:编辑你的表单和视图

对于Google ReCaptcha V2(不可见)

要为你的表单启用Recaptcha(不可见)保护,你必须使用此类型:MaelRecaptchaSubmitType::class

// For example
->add('captcha', MaelRecaptchaSubmitType::class, [
    'label' => 'Submit',
    'constraints' => new MaelRecaptcha()
])

对于第三个参数,它是一个数组,你可以添加约束:MaelRecaptcha

然后,为了完成不可见Recaptcha的配置,在你的twig文件中你需要给你的表单添加一个id

{{ form_start(your_form, {'attr': {'id': 'form-recaptcha'}}) }}

最后,添加2个<script>标签

将"your-form-id"替换为刚刚添加到你表单上的类名

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
   function onSubmitCaptcha(token) {
       document.getElementById("class-of-your-form").submit();
   }
</script>

注意,你无法更改JavaScript函数的名称。

对于Google ReCaptcha V2(复选框)

要为你的表单启用Recaptcha(复选框)保护,你必须使用此类型:MaelRecaptchaCheckboxType::class

->add('captcha_checkvox', MaelRecaptchaCheckboxType::class, [
    'constraints' => new MaelRecaptcha()
])

对于第三个参数,它是一个数组,你可以添加约束:MaelRecaptcha

完成,在你的twig文件中添加<script>标签

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

贡献

贡献者列表在这里

你想贡献?将该仓库Fork并创建一个pull request后更改

许可

你可以在根目录中找到许可