ethsam/easy-hcaptcha 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ethsam/easy-hcaptcha

Composer 安装命令:

composer require ethsam/easy-hcaptcha

包简介

This package provides an easy way to integrate hCaptcha into your Symfony application.

README 文档

README

This package provides an easy way to integrate hCaptcha into your Symfony application.

Installation

composer require ethsam/easy-hcaptcha

Configuration

Add the following to your .env file:

HCAPTCHA_SITE_KEY=your_site_key
HCAPTCHA_SECRET_KEY=your_secret_key

Add the following to config/packages/hcaptcha.yaml:

parameters:
    hcaptcha_site_key: '%env(HCAPTCHA_SITE_KEY)%'
    hcaptcha_secret_key: '%env(HCAPTCHA_SECRET_KEY)%'

Usage

In your Twig template, include the hCaptcha widget and script:

<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
    <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
</head>
<body>
    <form class="cons-contact-form" method="post" action="{{ path('app_contact_demande') }}">
        <!-- Other form fields -->
        <div class="h-captcha" data-sitekey="{{ hcaptcha_site_key }}"></div>
        <button type="submit">Envoyer</button>
    </form>
</body>
</html>

In your controller, verify the captcha response:

use Ethsam\EasyHcaptcha\HcaptchaVerifier;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class ContactController extends AbstractController
{
    private $mailer;
    private $hcaptchaVerifier;
    private $params;

    public function __construct(MailerInterface $mailer, HcaptchaVerifier $hcaptchaVerifier, ParameterBagInterface $params)
    {
        $this->mailer = $mailer;
        $this->hcaptchaVerifier = $hcaptchaVerifier;
        $this->params = $params;
    }

    #[Route('/nous-contacter', name: 'app_contact')]
    public function index(): Response
    {
        return $this->render('contact/index.html.twig', [
            'controller_name' => 'ContactController',
            'title' => 'Nous contacter',
            'contactLink' => true,
            'hcaptcha_site_key' => $this->params->get('hcaptcha_site_key'),
        ]);
    }

    #[Route('/votre-demande', name: 'app_contact_demande', methods: "POST")]
    public function formContact(Request $request)
    {
        $dataArray = [
            "user" => $request->request->get('user'), //just for bot
            "username" => $request->request->get('username'),
            "email" => $request->request->get('email'),
            "raisonsocial" => $request->request->get('raisonsocial'),
            "phone" => $request->request->get('phone'),
            "objet" => $request->request->get('objet'),
            "message" => $request->request->get('message'),
            "captcha" => $request->request->get('h-captcha-response'),
        ];

        if (empty($dataArray['user']) && !empty($dataArray['username']) && !empty($dataArray['email']) && !empty($dataArray['raisonsocial']) && !empty($dataArray['phone']) && !empty($dataArray['message']) && !empty($dataArray['captcha'])) {
            $captchaVerification = $this->hcaptchaVerifier->verify($dataArray['captcha']);

            if ($captchaVerification) {
                // Process the form
            } else {
                // Handle the error
            }
        } else {
            // Handle the error
        }

        return $this->render('contact/demande.html.twig', [
            'controller_name' => 'ItemsController',
            'noIndex' => true,
            'contactLink' => true,
            'title' => 'Votre demande',
            'statusMail' => $statusSend,
            'hcaptcha_site_key' => $this->params->get('hcaptcha_site_key'),
        ]);
    }
}

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-22