mael / recaptcha-bundle
为Symfony和Twig提供Google Recaptcha集成(易于与Symfony表单集成)
Requires
- php: ^7.1
- google/recaptcha: ^1.2
- symfony/config: ^4.4|^5.0
- symfony/dependency-injection: ^4.4|^5.0
- symfony/form: ^4.4|^5.0
- symfony/http-foundation: ^4.4|^5.0.7
- symfony/http-kernel: ^4.4|^5.0
- symfony/options-resolver: ^4.4|^5.0
- symfony/twig-pack: *
- symfony/validator: ^4.4|^5.0
Requires (Dev)
- phpunit/phpunit: ^9.1
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
运行以下命令
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后更改
许可
你可以在根目录中找到许可