Installation
composer require l3aro/filament-turnstile
Publish the config (if needed):
php artisan vendor:publish --provider="L3aro\FilamentTurnstile\FilamentTurnstileServiceProvider" --tag="config"
Register the Plugin
Add to app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
\L3aro\FilamentTurnstile\FilamentTurnstilePlugin::make(),
]);
}
Configure Cloudflare Turnstile
Add your site key and secret to .env:
FILAMENT_TURNSTILE_SITE_KEY=your_site_key
FILAMENT_TURNSTILE_SECRET_KEY=your_secret_key
First Use Case Add Turnstile to a form in Filament:
use L3aro\FilamentTurnstile\Components\Turnstile;
Turnstile::make('turnstile')
->required()
->rules(['required', 'turnstile'])
->columnSpanFull(),
Form Integration
Use Turnstile component in Filament forms (e.g., create/edit pages):
public static function form(Form $form): Form
{
return $form
->schema([
// ... other fields
Turnstile::make('turnstile')
->label('Verify You Are Human')
->required(),
]);
}
Custom Validation Extend validation rules in your model:
use Illuminate\Validation\Rules\Turnstile;
public static function rules(): array
{
return [
'turnstile' => ['required', Turnstile::make()],
];
}
Dynamic Configuration Override plugin settings per panel:
FilamentTurnstilePlugin::make()
->siteKey(env('FILAMENT_TURNSTILE_SITE_KEY_ALTERNATE'))
->theme('dark'),
API Integration Verify Turnstile responses in API routes:
use L3aro\FilamentTurnstile\Facades\Turnstile;
public function store(Request $request)
{
$validated = $request->validate([
'turnstile' => ['required', Turnstile::make()],
]);
// ...
}
Multi-Language Support Localize Turnstile labels:
Turnstile::make('turnstile')
->label(__('filament-turnstile::messages.verify'))
->hint(__('filament-turnstile::messages.hint')),
Missing .env Configuration
FILAMENT_TURNSTILE_SITE_KEY and FILAMENT_TURNSTILE_SECRET_KEY are set in .env.CORS Issues
Caching Stale Responses
php artisan filament:cache:clear) or use Turnstile::make()->ignoreCache() for testing.Theme Mismatch
Turnstile::make()->theme('dark'),
Rate Limiting
error-captcha-exhausted.FILAMENT_TURNSTILE_SECRET_KEY permissions.Enable Debug Mode
Add to .env:
FILAMENT_TURNSTILE_DEBUG=true
Logs verification responses to storage/logs/filament-turnstile.log.
Test Locally Use Cloudflare’s Turnstile test mode with:
FILAMENT_TURNSTILE_TEST_MODE=true
Inspect Network Requests
Check the POST /cdn-cgi/turnstile/v0/siteverify request in browser dev tools for errors.
Custom Verification Logic Override the default verification by binding your own validator:
Turnstile::make()->validator(function ($response) {
// Custom logic here
return $response['success'];
});
Event Listeners
Listen for Turnstile events (e.g., TurnstileVerified):
use L3aro\FilamentTurnstile\Events\TurnstileVerified;
TurnstileVerified::listen(function ($model, $response) {
// Log or process the response
});
Blade Integration
Use the @turnstile directive in custom Blade views:
@turnstile('turnstile', [
'theme' => 'light',
'size' => 'normal',
])
Testing Mock Turnstile responses in tests:
Turnstile::shouldReceive('verify')
->once()
->andReturn(['success' => true, 'error-codes' => []]);
How can I help you explore Laravel packages today?