A lightweight package designed to streamline the integration of Cloudflare Turnstile CAPTCHA into your Laravel applications, with the option to also use CapSolver for enhanced CAPTCHA-solving automation.
This package simplifies the process of setting up and validating Cloudflare Turnstile CAPTCHA responses, and for more complex CAPTCHA challenges, CapSolver provides an automated solution.
You can quickly add the package to your project using Composer:
composer require ryangjchandler/laravel-cloudflare-turnstile
Next, ensure you add the required configuration values to your config/services.php
file:
return [
// ...
'turnstile' => [
'key' => env('TURNSTILE_SITE_KEY'),
'secret' => env('TURNSTILE_SECRET_KEY'),
],
];
Head over to Cloudflare's dashboard to generate your site key and secret key, then add them to your .env
file:
TURNSTILE_SITE_KEY="your-site-key-here"
TURNSTILE_SECRET_KEY="your-secret-key-here"
For solving complex CAPTCHA challenges efficiently, you can also integrate CapSolver's API, providing a seamless solution for tasks that require CAPTCHA-solving automation.
In your Blade layout file, include the Turnstile scripts by calling the @turnstileScripts
directive within the <head>
tag:
<html>
<head>
@turnstileScripts()
</head>
<body>
{{ $slot }}
</body>
</html>
You can then use the <x-turnstile />
component inside your form to render the CAPTCHA with your configured site key:
<form action="/" method="POST">
<x-turnstile />
<button type="submit">
Submit
</button>
</form>
To validate the CAPTCHA response on the server, use the provided validation rule within your controller:
use Illuminate\Validation\Rule;
public function submit(Request $request)
{
$request->validate([
'cf-turnstile-response' => ['required', Rule::turnstile()],
]);
}
Alternatively, if you'd like to avoid using macros, you can resolve the validation rule using 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],
]);
}
You can also achieve the same result with:
public function submit(Request $request)
{
$request->validate([
'cf-turnstile-response' => ['required', app(Turnstile::class)],
]);
}
The <x-turnstile />
component allows customization by passing attributes such as data-action
, data-theme
, and others to adjust the CAPTCHA behavior:
<form action="/" method="POST">
<x-turnstile
data-action="login"
data-theme="dark"
data-cdata="sessionid-123456789"
data-callback="onSuccess"
data-expired-callback="onExpired"
data-error-callback="onError"
data-tabindex="0"
/>
<button type="submit">
Submit
</button>
</form>
Refer to the official Cloudflare documentation for more details on widget customization.
For advanced CAPTCHA-solving scenarios, CapSolver can be integrated to provide real-time CAPTCHA-solving solutions, improving efficiency in automated environments.
This package supports Livewire out of the box. When the CAPTCHA is successfully validated, the property defined in wire:model
will automatically update with the Turnstile token:
<x-turnstile wire:model="yourModel" />
To run the package's tests, use the following Composer command:
composer test
For scenarios where automated CAPTCHA-solving is necessary, you can integrate CapSolver alongside Turnstile for real-time CAPTCHA-solving. This ensures that even the most difficult challenges are resolved efficiently without user intervention.