maxpowel / wixet-recaptcha-bundle
此包为 symfony 表单提供对 google recaptcha 的支持
v1.1
2017-02-21 19:17 UTC
Requires
- php: ^5.5.9|^7.0
- symfony/symfony: >=2.3
Requires (Dev)
- php: ^5.5.9|^7.0
- symfony/symfony: 3.2.*
This package is not auto-updated.
Last update: 2024-09-28 20:30:32 UTC
README
轻松将 google captcha 验证添加到 symfony 表单
安装
步骤 1: 使用 composer 启用 Bundle
要在终端中安装 WixetRecaptchaBundle,只需输入 composer 命令
composer require maxpowel/wixet-recaptcha-bundle
现在,Composer 将自动下载所有必要的文件,并为您安装它们。接下来需要做的就是更新您的 AppKernel.php
文件,并注册新的 Bundle
<?php // in AppKernel::registerBundles() $bundles = array( // ... new Wixet\RecaptchaBundle\WixetRecaptchaBundle(), // ... );
步骤 2: 配置 Bundle
将以下内容添加到您的配置文件中
wixet_recaptcha: site_key: "YourSiteKey" secret: "YourSecret"
在 https://www.google.com/recaptcha/admin 获取您的 recaptcha 密钥
基本用法
在您的控制器或表单所在的任何地方,添加 WixetRecaptchaType,如下例所示
$form = $this->createFormBuilder() ->add("name", TextType::class) ->add("recaptcha", WixetRecaptchaType::class) ->add("Submit", SubmitType::class) ->getForm() ;
别忘了包含类型
use Wixet\RecaptchaBundle\Form\Type\WixetRecaptchaType;
多选和延迟渲染
有时您需要多个 recaptcha 或延迟渲染(不是在页面加载时渲染,而是在您需要时渲染)。这在您需要加载带有 recaptcha 的 AJAX 内容时非常有用。
第一步是手动包含 recaptcha 脚本(在您加载所有脚本的地方),您有两种选择
{{ include_recaptcha() | raw }}
或
{{ include('WixetRecaptchaBundle::recaptcha_explicit.html.twig', { 'site_key': recaptcha_site_key() }) }}
我更喜欢第一种方式,但第二种方式允许更多的自定义。
现在以通常的方式渲染表单,但带有 "explicit_render=true" 选项
$form = $this->createFormBuilder() ->add("name", TextType::class) ->add("recaptcha", WixetRecaptchaType::class, array( 'explicit_render' => true )) ->add("Submit", SubmitType::class) ->getForm() ;
就是这样!