https://github.com/zemasterkrom/zmkr-cloudflare-turnstile-bundle
Extensible and configurable integration of Cloudflare Turnstile with Symfony
https://github.com/zemasterkrom/zmkr-cloudflare-turnstile-bundle
anti-spam bundle captcha cloudflare form security symfony turnstile
Last synced: about 1 year ago
JSON representation
Extensible and configurable integration of Cloudflare Turnstile with Symfony
- Host: GitHub
- URL: https://github.com/zemasterkrom/zmkr-cloudflare-turnstile-bundle
- Owner: Zemasterkrom
- License: mit
- Created: 2023-10-30T18:40:23.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-26T21:58:59.000Z (over 1 year ago)
- Last Synced: 2025-04-30T07:11:54.284Z (about 1 year ago)
- Topics: anti-spam, bundle, captcha, cloudflare, form, security, symfony, turnstile
- Language: PHP
- Homepage:
- Size: 151 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ZmkrCloudflareTurnstileBundle
=============================
[](https://github.com/zemasterkrom/zmkr-cloudflare-turnstile-bundle/actions)
[](https://codecov.io/gh/Zemasterkrom/zmkr-cloudflare-turnstile-bundle/)
Requires **Symfony `>= 5.0`** and **PHP `>= 7.4`**. Tested up to **Symfony 7**.
The purpose of this bundle is to facilitate the configuration and integration of the **Cloudflare Turnstile** captcha system into **Symfony** forms.
Summary of features provided:
- Automatic captcha rendering, language configuration and validation
- Timeout management
- Client-side error domain management
- reCAPTCHA / hCaptcha compatibility mode
- Possibility of using custom JavaScript rendering logic
- Possibility of marking captchas as required
Get started
-----------
### With Symfony Flex
Run the following command with **Composer**:
`composer require zemasterkrom/zmkr-cloudflare-turnstile-bundle`
If you have not explicitly allowed "contrib" recipes in your *composer.json* file, you will be prompted with the following message for `zemasterkrom/zmkr-cloudflare-turnstile-bundle`:
> Do you want to execute this recipe?
Answer **yes** for `zemasterkrom/zmkr-cloudflare-turnstile-bundle`.
Finally, modify the `zemasterkrom/zmkr-cloudflare-turnstile-bundle` section of the `.env` file to add your **Cloudflare Turnstile** application keys:
```
###> zemasterkrom/zmkr-cloudflare-turnstile-bundle ###
CLOUDFLARE_TURNSTILE_SITEKEY=
CLOUDFLARE_TURNSTILE_SECRET_KEY=
###< zemasterkrom/zmkr-cloudflare-turnstile-bundle ###
```
### Without Symfony Flex
Install **Composer** dependencies:
`composer require zemasterkrom/zmkr-cloudflare-turnstile-bundle`
Register the bundle inside the `config/bundles.php` file:
```php
return [
//...
Zemasterkrom\CloudflareTurnstileBundle\ZmkrCloudflareTurnstileBundle::class => ['all' => true],
];
```
Create the `config/packages/zmkr_cloudflare_turnstile.yaml` configuration file:
```yaml
zmkr_cloudflare_turnstile:
captcha:
sitekey: "%env(CLOUDFLARE_TURNSTILE_SITEKEY)%"
secret_key: "%env(CLOUDFLARE_TURNSTILE_SECRET_KEY)%"
```
Add your **Cloudflare Turnstile** application keys to the `.env` file:
```
CLOUDFLARE_TURNSTILE_SITEKEY=
CLOUDFLARE_TURNSTILE_SECRET_KEY=
```
Usage
-----
To add a **Cloudflare Turnstile** captcha to a form, you need to associate a `CloudflareTurnstileType` field in your form builder:
```php
add('first_name', TextType::class, [
'label' => 'First name'
])
->add('last_name', TextType::class, [
'label' => 'Last name'
])
->add('cf_turnstile_response', CloudflareTurnstileType::class);
}
}
```
Validation will be performed automatically when you check the form that is associated with a ``CloudflareTurnstileType``:
```php
createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// The form has been submitted and is valid: execute your actions here
}
// The form has not been submitted or is invalid: if there is an error, an error message will be registered in your form
return $this->render('', [
'form' => $form->createView(),
]);
}
}
```
Documentation
-------------
For advanced usage, please see the [documentation](docs/index.md).
License
-------
This bundle is licensed under the MIT license. The license is accessible [here](LICENSE).