PHPackages                             kaistaerk/hcaptcha-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kaistaerk/hcaptcha-bundle

ActiveSymfony-bundle

kaistaerk/hcaptcha-bundle
=========================

A Symfony bundle to use hCaptcha in forms

v4.0.2(2y ago)0295↓18.9%MITPHPPHP ^8.1

Since Jul 9Pushed 2y agoCompare

[ Source](https://github.com/kaistaerk/hcaptcha-bundle)[ Packagist](https://packagist.org/packages/kaistaerk/hcaptcha-bundle)[ RSS](/packages/kaistaerk-hcaptcha-bundle/feed)WikiDiscussions v4 Synced 1mo ago

READMEChangelogDependencies (19)Versions (24)Used By (0)

HCaptcha bundle for Symfony 3+ [![Symfony](https://github.com/Meteo-Concept/hcaptcha-bundle/actions/workflows/symfony.yml/badge.svg?branch=v4)](https://github.com/Meteo-Concept/hcaptcha-bundle/actions/workflows/symfony.yml)
===============================================================================================================================================================================================================================

[](#hcaptcha-bundle-for-symfony-3-)

This bundle brings into your Symfony website a new Form type, namely HCaptchaType, that is used to display and validate a CAPTCHA served by .

This bundle is tested for Symfony major versions 3, 4, 5 and 6. Major version 2 works for Symfony 3 and 4, with PHP 7.2+ ; major version 3 for Symfony 5 and 6 with PHP 7.4 or 8.x ; major version 4 for Symfony 6.4 and 7 with PHP 8.2+. The test dependencies requirements can be more stringent.

Installation
------------

[](#installation)

### Applications that use Symfony Flex

[](#applications-that-use-symfony-flex)

Open a command console, enter your project directory and execute:

```
$ composer require meteo-concept/hcaptcha-bundle
```

In order to avoid making you install another HTTP client if you already have a compatible one, this bundle depends on *virtual* packages, namely PSR-18 psr/http-client-interface and PSR-17 psr/http-factory-interface. If you don't have any real package already installed in your application providing an implementation for these, composer will complain that the bundle is not installable. In this case, you have to provide a real implementation at the same time as the bundle.

For instance, starting from Symfony 4:

```
$ composer require meteo-concept/hcaptcha-bundle symfony/http-client nyholm/psr7
```

For Symfony 3:

```
$ composer require meteo-concept/hcaptcha-bundle guzzlehttp/guzzle nyholm/psr7
```

### Applications that don't use Symfony Flex

[](#applications-that-dont-use-symfony-flex)

#### Step 1: Download the Bundle

[](#step-1-download-the-bundle)

Install the bundle with one of the commands above. You now have to enable it and configure it without the recipe.

#### Step 2: Enable the Bundle

[](#step-2-enable-the-bundle)

Then, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```
// config/bundles.php

return [
    // ...
    MeteoConcept\HCaptchaBundle\MeteoConceptHCaptchaBundle::class => ['all' => true],
];
```

Configuration
-------------

[](#configuration)

This captcha is provided with a Symfony flex contrib recipe so it should come with a configuration if you have those enabled. Otherwise, you can copy the configuration from the contrib repository: .

Configure the bundle, for instance in `config/packages/meteo_concept_hcaptcha.yml`:

```
parameters:
    hcaptcha_site_key: '%env(resolve:HCAPTCHA_SITE_KEY)%'
    hcaptcha_secret: '%env(resolve:HCAPTCHA_SECRET)%'

meteo_concept_h_captcha:
  hcaptcha:
    site_key: '%hcaptcha_site_key%'
    secret: '%hcaptcha_secret%'
  validation: 'strict' # this is the default
```

with the corresponding change in `.env`:

```
HCAPTCHA_SITE_KEY="10000000-ffff-ffff-ffff-000000000001"
HCAPTCHA_SECRET="0x0000000000000000000000000000000000000000"
```

The site key and secret are the values hCaptcha gives you at . The global configuration makes all captchas use the same site key by default but it's possible to change it in the definition of each form.

The values shown here are dummy values usable for integration testing (). Put the real values in `.env.local` (at least, the secret, the site key is public).

The validation can be set to 'strict' or 'lax'. If it's 'lax', then the CAPTCHA will be considered valid even if the hCaptcha endpoint times out or return a HTTP 500 error for instance (so as to not frustrate the users too much). If it's strict (the default), then the CAPTCHA will not be considered valid unless the endpoint returns a "success: true" answer.

Configure Twig to load the specific template for the hCaptcha widget (or provide your own).

```
twig:
    ...
    form_themes:
        - '@MeteoConceptHCaptcha/hcaptcha_form.html.twig'
        - ...
```

If you use Guzzle or another HTTP library, you may also need a configuration for that bundle and its services. For instance, for Guzzle, you probably need the following in `services.yaml`:

```
services:
    Psr\Http\Client\ClientInterface:
        class: GuzzleHttp\Client
    Psr\Http\Message\RequestFactoryInterface:
        class: Nyholm\Psr7\Factory\Psr17Factory
    Psr\Http\Message\StreamFactoryInterface:
        class: Nyholm\Psr7\Factory\Psr17Factory
```

Usage
-----

[](#usage)

Use the captcha in your forms:

```
