phpmob/recaptcha-bundle

使用 Google reCAPTCHA 进行垃圾邮件保护

1.1.1 2018-02-05 10:26 UTC

This package is auto-updated.

Last update: 2024-09-18 23:33:56 UTC


README

PhpMobReCaptchaBundle 是一个使用 Google reCaptcha 进行垃圾邮件保护的工具。

安装

建议使用 composer 进行安装。

"require": {
  "phpmob/recaptcha-bundle": "~1.1"
}

启用

然后在 AppKernel.php 中启用 bundle。

public function registerBundles()
{
    $bundles = [
        ...
        new \PhpMob\ReCaptchaBundle\PhpMobReCaptchaBundle(),
    ];
}

使用方法

为了使用它,您需要进行一些配置。首先,通过 https://www.google.com/recaptcha/admin 创建 Google reCaptcha,然后在您的 symfony 应用中进行配置。

  1. 配置
phpmob_recaptcha
    site_key: <google_recaptcha_site_key>
    secret_key: <google_recaptcha_secret_key>
    # optional
    enabled: true/false # toggle enable or disable to using it
    verify_host: true/false # strict verify hostname (not allow to submit from remote host)
    theme: light/dark # default light
  1. 使用
<?php

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use PhpMob\ReCaptchaBundle\Form\Type\RecaptchaType;
use PhpMob\ReCaptchaBundle\Validator\Constraints\IsValid;

class YourType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field', TextType::class, [])

            ->add("recaptcha", RecaptchaType::class, [
                'mapped' => false,
                'label' => false,
                "constraints" => [
                    new IsValid(['groups' => ['some_group_if_need']])
                ]
            ])
        ;
    }
}

2.1 与登录表单一起使用

phpmob_recaptcha
    login:
        enabled: true
        firewall: your_firewall_section_name
<?php

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use PhpMob\ReCaptchaBundle\Form\Type\RecaptchaType;
use PhpMob\ReCaptchaBundle\Validator\Constraints\IsValid;

class UserLoginType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('_username', TextType::class, [])
            ->add('_password', PasswordType::class, [])

            ->add("recaptcha", RecaptchaType::class, [
                'mapped' => false,
                'label' => false,
            ])
        ;
    }
}

这就完成了!

许可证

MIT