javadle / laravel-cloudflare-turnstile
A simple package to help integrate Cloudflare Turnstile.
Requires
- php: ^8.0
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2025-01-08 02:34:50 UTC
README
This packages provides helper for setting up and validating Cloudflare Turnstile CAPTCHA responses.
Installation
You can install the package via Composer:
composer require ryangjchandler/laravel-cloudflare-turnstile
You should then add the following configuration values to your config/services.php
file:
return [ // ..., 'turnstile' => [ 'key' => env('TURNSTILE_SITE_KEY'), 'secret' => env('TURNSTILE_SECRET_KEY'), ], ];
Visit Cloudflare to create your site key and secret key and add them to your .env
file.
TURNSTILE_SITE_KEY="1x00000000000000000000AA"
TURNSTILE_SECRET_KEY="2x0000000000000000000000000000000AA"
Usage
In your layout file, include the Turnstile scripts using the @turnstileScripts
Blade directive. This should be added to the <head>
of your document.
<html> <head> @turnstileScripts() </head> <body> {{ $slot }} </body> </html>
Once that's done, you can use the @turnstile
directive in <form>
to output the appropriate markup with your site key configured.
<form action="/" method="POST"> @turnstile() <button> Submit </button> </form>
On the server, use the provided validation rule to validate the CAPTCHA response.
use Illuminate\Validation\Rule; public function submit(Request $request) { $request->validate([ 'cf-turnstile-response' => ['required', Rule::turnstile()], ]); }
If you prefer to not use a macro, you can resolve an instance of the rule from the container via dependency injection or the app()
helper.
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile; public function submit(Request $request, Turnstile $turnstile) { $request->validate([ 'cf-turnstile-response' => ['required', $turnstile], ]); }
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile; public function submit(Request $request) { $request->validate([ 'cf-turnstile-response' => ['required', app(Turnstile::class)], ]); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.