vihuvac / recaptcha-bundle
此扩展包为Symfony项目提供了轻松的reCAPTCHA表单字段集成
Requires
- php: ^5.3 || ^7.0
- symfony/symfony: ^2.3 || ^3.0
Requires (Dev)
- phpunit/phpunit: ^6.5
README
⚠️ 已弃用
很遗憾地通知,此扩展包即将被弃用,建议使用 EWZRecaptchaBundle 代替。对此造成的不便表示歉意。😔
此扩展包为Symfony提供简单的reCAPTCHA表单字段,以保护您的网站免受垃圾邮件和滥用。
安装
步骤1:使用composer启用扩展包
要使用composer安装扩展包,只需在命令行(终端)中运行
$ composer require vihuvac/recaptcha-bundle
Composer会自动下载所有必需的文件,并为您安装。接下来,您需要更新您的AppKernel.php
文件,并注册新的扩展包
// app/AppKernel.php <?php public function registerBundles() { $bundles = array( // ... new Vihuvac\Bundle\RecaptchaBundle\VihuvacRecaptchaBundle(), ); }
步骤2:配置扩展包
将以下内容添加到您的配置文件中
# app/config/config.yml vihuvac_recaptcha: site_key: here_is_your_site_key secret_key: here_is_your_secret_key locale_key: kernel.default_locale
注意:
此扩展包使用安全API(HTTPS协议)。Google API通过浏览器(客户端)解决请求。
site_key
参数与public_key
参数相同,secret_key
参数与private_key
参数相同(在旧版本中使用的参数)。
您可以通过使用布尔值true
或false
来轻松启用或禁用reCAPTCHA功能,例如
# app/config/config.yml vihuvac_recaptcha: // ... enabled: true
如果您想使用请求的本地化所使用的语言作为reCAPTCHA的语言,您必须激活解析器(默认情况下已禁用)
# app/config/config.yml vihuvac_recaptcha: // ... locale_from_request: true
您可以使用Ajax API(可选)加载reCAPTCHA
# app/config/config.yml vihuvac_recaptcha: // ... ajax: true
此外,您还可以添加HTTP代理配置(可选)
# app/config/config.yml vihuvac_recaptcha: // ... host: proxy.your-domain.com port: 3128 auth: proxy_username:proxy_password
如果您已在reCAPTCHA端关闭域名检查,您需要通过启用verify_host
选项来检查响应的来源
# app/config/config.yml vihuvac_recaptcha: // ... verify_host: true
恭喜!您已准备就绪!
基本用法
在创建新的表单类时,请添加以下行以创建字段
Symfony和PHP参考
<?php public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add("recaptcha", "vihuvac_recaptcha"); // ... }
Symfony和PHP参考
注意:
要表示表单类型,您必须使用完全限定的类名 - 如PHP 5.5+中的
TextType::class
或Symfony\Component\Form\Extension\Core\Type\TextType
。在Symfony 2.8之前,您可以像text
或date
一样为每个类型使用别名。旧别名语法在Symfony 3.0之前仍然有效。有关更多详细信息,请参阅2.8升级日志。
<?php use Vihuvac\Bundle\RecaptchaBundle\Form\Type\VihuvacRecaptchaType as RecaptchaType; public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add("recaptcha", RecaptchaType::class); // ... }
您可以通过attr > options
选项传递额外的选项到reCAPTCHA,例如
<?php use Vihuvac\Bundle\RecaptchaBundle\Form\Type\VihuvacRecaptchaType as RecaptchaType; public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add("recaptcha", RecaptchaType::class, array( "attr" => array( "options" => array( "theme" => "light", "type" => "audio", "size" => "normal", "defer" => false, // Set true if you want to use the Ajax API. "async" => false // Set true if you want to use the Ajax API. ) ) )); // ... }
reCAPTCHA标签属性和渲染参数
支持Google的无形reCAPTCHA!非常简单
<?php use Vihuvac\Bundle\RecaptchaBundle\Form\Type\VihuvacRecaptchaType as RecaptchaType; public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add("recaptcha", RecaptchaType::class, array( "attr" => array( "options" => array( "theme" => "light", "type" => "image", "size" => "invisible", // Set size to the invisible reCAPTCHA. "defer" => false, // Set true if you are using the Ajax API. "async" => false, // Set true if you are using the Ajax API. "callback" => "onReCaptchaSuccess", // Callback will be set by default if it's not defined (along with JS function that validates the form on success). "bind" => "buttonSubmit", // This is the form submit button id (html attribute). // ... ) ) )); // ... }
注意:如果您使用预定义的回调,则需要将
recaptcha-form
类添加到您的<form>
标签中。
如果您需要根据您的站点语言配置reCAPTCHA的语言(适用于多语言网站),您可以通过“language”选项传递语言
<?php use Vihuvac\Bundle\RecaptchaBundle\Form\Type\VihuvacRecaptchaType as RecaptchaType; public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add("recaptcha", RecaptchaType::class, array( "language" => "en", // ... )); // ... }
要验证字段,请使用
<?php use Vihuvac\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha; /** * @Recaptcha\IsTrue */ public $recaptcha;
另一种方法是将验证约束作为FormType的选项传递。这样,您的数据类只包含有意义的属性。以上面的例子为例,buildForm方法将如下所示。请注意,如果设置mapped => false
,则注释将不起作用。您还必须设置constraints
<?php use Vihuvac\Bundle\RecaptchaBundle\Form\Type\VihuvacRecaptchaType as RecaptchaType; use Vihuvac\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue; public function buildForm(FormBuilder $builder, array $options) { // ... $builder->add("recaptcha", RecaptchaType::class, array( "attr" => array( "options" => array( "theme" => "light", "type" => "audio", "size" => "normal" ) ), "mapped" => false, "constraints" => array( new RecaptchaTrue() ) )); // ...
酷!表单模板资源现在通过容器扩展自动注册。然而,您始终可以实现自己的自定义表单小部件。
PHP:
<?php $view["form"]->setTheme($form, array("VihuvacRecaptchaBundle:Form")) ?> <?php echo $view["form"]->widget($form["recaptcha"], array( "attr" => array( "options" => array( "theme" => "light", "type" => "audio", "size" => "normal" ) ) )) ?>
Twig:
{% form_theme form "VihuvacRecaptchaBundle:Form:vihuvac_recaptcha_widget.html.twig" %} {{ form_widget( form.recaptcha, { "attr": { "options": { "theme": "light", "type": "audio", "size": "normal" }, } } ) }}
如果您不使用表单,您仍然可以使用JavaScript实现reCAPTCHA字段。
PHP:
<div id="recaptcha-container"></div> <script type="text/javascript"> $(document).ready(function() { $.getScript("<?php echo \Vihuvac\Bundle\RecaptchaBundle\Form\Type\VihuvacRecaptchaType::RECAPTCHA_API_JS_SERVER ?>", function() { Recaptcha.create("<?php echo $form['recaptcha']->get('site_key') ?>", "recaptcha-container", { theme: "light", type: "audio", "size": "normal" }); }); }; </script>
Twig:
<div id="recaptcha-container"></div> <script type="text/javascript"> $(document).ready(function() { $.getScript("{{ constant('\\Vihuvac\\Bundle\\RecaptchaBundle\\Form\\Type\\VihuvacRecaptchaType::RECAPTCHA_API_JS_SERVER') }}", function() { Recaptcha.create("{{ form.recaptcha.get('site_key') }}", "recaptcha-container", { theme: "light", type: "audio", "size": "normal" }); }); }); </script>
定制:
如果您想使用自定义主题,请将您的代码块放在设置主题之前。
<div id="recaptcha_widget"> <div id="recaptcha_image"></div> <div class="recaptcha_only_if_incorrect_sol" style="color:red">Incorrect please try again</div> <span class="recaptcha_only_if_image">Enter the words above:</span> <span class="recaptcha_only_if_audio">Enter the numbers you hear:</span> <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" /> <div><a href="javascript:Recaptcha.reload()">Get another CAPTCHA</a></div> <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type("audio")">Get an audio CAPTCHA</a></div> <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type("image")">Get an image CAPTCHA</a></div> <div><a href="javascript:Recaptcha.showhelp()">Help</a></div> </div> {% form_theme form "VihuvacRecaptchaBundle:Form:vihuvac_recaptcha_widget.html.twig %} {{ form_widget( form.recaptcha, { "attr": { "options" : { "theme" : "custom", }, } } ) }}
进一步阅读: 谷歌官方文档。
测试
执行此命令以运行测试
$ cd recaptcha-bundle/
$ ./vendor/bin/phpunit
注意:如果您只运行测试并且仅在包内运行,作为第一步,您应该运行
composer install
以安装所需的依赖。然后您就可以运行测试了!