lambda-studio/turnstile
Laravel package for Cloudflare Turnstile captcha validation. Includes a ValidTurnstile validation rule to verify the cf-turnstile-response token in requests, plus a simple Blade form example using your configured site key and Turnstile script.
ValidTurnstile rule that integrates seamlessly with Laravel’s form request validation system. This aligns perfectly with Laravel’s declarative validation approach, reducing boilerplate and improving maintainability.Turnstile::verify()) abstracts API calls, promoting clean, testable code.ValidTurnstile rule requires minimal setup—just apply it to form fields (e.g., cf-turnstile-response). This leverages Laravel’s existing validation infrastructure without disrupting workflows.data-sitekey are included. This flexibility avoids locking the project into a specific frontend framework.config/turnstile.php, adhering to Laravel’s conventions for environment-specific configurations (e.g., .env files). This simplifies deployment across environments.ValidateTurnstile middleware adds latency if applied globally. Evaluate performance impact in high-traffic routes (e.g., admin dashboards) and restrict it to critical paths.@turnstile()) are planned but not implemented. If these are critical, consider implementing them manually or waiting for the package’s next release./contact, /login)? Global middleware may impact performance.Http::fake() or similar tools to simulate API responses.config('turnstile.secret') values secured? Use environment variables (.env) or a secrets manager (e.g., Laravel Forge, Vault).Installation:
composer require lambda-studio/turnstile
Publish the configuration file:
php artisan vendor:publish --provider="LambdaStudio\Turnstile\TurnstileServiceProvider"
Update config/turnstile.php with your Cloudflare Turnstile site_key and secret_key from .env.
Configuration:
Add the following to your .env file:
TURNSTILE_SITE_KEY=your_site_key
TURNSTILE_SECRET_KEY=your_secret_key
Validation Integration:
Apply the ValidTurnstile rule to form fields in your request validation logic:
use LambdaStudio\Turnstile\Rules\ValidTurnstile;
$request->validate([
'cf-turnstile-response' => [
'required',
'string',
new ValidTurnstile(),
],
]);
Middleware Integration (Optional):
Register the middleware in app/Http/Kernel.php for global validation:
protected $middleware = [
// ...
\LambdaStudio\Turnstile\Http\Middleware\ValidateTurnstile::class,
];
Or apply it to specific routes:
Route::middleware(['turnstile'])->group(function () {
// Routes requiring Turnstile validation
});
Frontend Implementation: Include the Turnstile script and widget in your forms. For Blade templates:
<div class="cf-turnstile" data-sitekey="{{ config('turnstile.site_key') }}"></div>
@error('cf-turnstile-response')
<span>{{ $message }}</span>
@enderror
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
ValidTurnstile rule.@csrf directive without conflicts. Ensure both are included in forms.Phase 1: Core Validation
ValidTurnstile rule in critical forms (e.g., contact, registration, login).Phase 2: Middleware Integration
ValidateTurnstile middleware to routes or globally (if performance allows).Phase 3: API Integration (Optional)
Phase 4: Customization
@turnstile) if the package’s TODO items are critical.Phase 5: Monitoring and Optimization
How can I help you explore Laravel packages today?