l3aro/filament-turnstile
Composer 安装命令:
composer require l3aro/filament-turnstile
包简介
Filament Turnstile is an essential plugin designed to seamlessly integrate Cloudflare Turnstile into your Filament application.
README 文档
README
Filament Turnstile is a lightweight plugin that seamlessly integrates Cloudflare Turnstile with your Filament application.
Installation
Install the package via Composer:
composer require l3aro/filament-turnstile
Publish the configuration file with:
php artisan vendor:publish --tag="filament-turnstile-config"
Optionally publish the views with:
php artisan vendor:publish --tag="filament-turnstile-views"
The published configuration file contains:
return [ 'key' => env('TURNSTILE_SITE_KEY'), 'secret' => env('TURNSTILE_SECRET_KEY'), 'reset_event' => env('TURNSTILE_RESET_EVENT', 'reset-captcha'), ];
Customizing the Reset Event
You can customize the name of the reset event globally by modifying the TURNSTILE_RESET_EVENT in your .env file:
TURNSTILE_RESET_EVENT=your-custom-event-name
Or by directly modifying the reset_event value in the configuration file. This allows you to use a custom event name that better fits your application's event naming convention or to avoid conflicts with other JavaScript events.
Turnstile Keys
To use Cloudflare Turnstile, obtain your SiteKey and SecretKey from the Cloudflare dashboard.
Refer to the documentation for step-by-step instructions.
After you generate the keys, add them to your .env file in the following format:
TURNSTILE_SITE_KEY=1x00000000000000000000AA TURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA
For testing, you may use the dummy site and secret keys provided by Cloudflare.
Usage
Using the plugin is straightforward. In your form, add the following component:
use l3aro\FilamentTurnstile\Enums\TurnstileSize; use l3aro\FilamentTurnstile\Enums\TurnstileTheme; use l3aro\FilamentTurnstile\Forms\Turnstile; Turnstile::make('captcha') ->theme(TurnstileTheme::Auto) ->size(TurnstileSize::Normal) ->language('en-US') ->resetEvent('reset-captcha')
See the supported languages list for available locale codes.
The Turnstile field exposes additional options. Review the Cloudflare configuration guide for full details.
Turnstile Events
The package provides events you can use to control the captcha in different scenarios.
Reset event
The reset event (default: reset-captcha) resets the captcha challenge. It is helpful when you need to:
- Clear the challenge after a successful submission so the next visitor receives a fresh captcha.
- Reset the challenge after validation errors to avoid reusing a solved captcha when the form fails.
Dispatching the reset event
There are two primary ways to dispatch reset-captcha:
-
Via
onValidationError: Filament automatically callsonValidationErrorwhen validation fails. Dispatch the event there to refresh the captcha.use l3aro\FilamentTurnstile\Facades\FilamentTurnstileFacade; protected function onValidationError(ValidationException $exception): void { $this->dispatch(FilamentTurnstileFacade::getResetEventName()); // Perform additional actions as needed (e.g., display error messages) }
-
Manually: Dispatch the event whenever your logic requires a reset.
$this->dispatch(FilamentTurnstileFacade::getResetEventName());
Resetting the login captcha
To automatically reset the captcha after a failed login attempt, use the throwFailureValidationException method in your login form's Livewire component:
protected function authenticate(): void { // Perform authentication logic // ... if (! Auth::attempt($this->data)) { $this->throwFailureValidationException( [ 'email' => 'Invalid email or password.', ] ); } // Redirect to success page or perform other actions }
Throwing a validation exception triggers onValidationError, which dispatches reset-captcha and refreshes the captcha for the next attempt.
Real-life example
To implement the Turnstile captcha on the Login page in Filament, follow these steps:
Create a new App/Filament/Pages/Auth/Login.php class:
namespace App\Filament\Pages\Auth; use l3aro\FilamentTurnstile\Forms\Turnstile; use Filament\Forms\Form; use Filament\Http\Responses\Auth\Contracts\LoginResponse; use Filament\Pages\Auth\Login as AuthLogin; class Login extends AuthLogin { /** * @return array<int|string, string|Form> */ protected function getForms(): array { return [ 'form' => $this->form( $this->makeForm() ->schema([ $this->getEmailFormComponent(), $this->getPasswordFormComponent(), $this->getRememberFormComponent(), Turnstile::make('captcha'), ]) ->statePath('data'), ), ]; } // if you want to reset the captcha in case of validation error protected function throwFailureValidationException(): never { $this->dispatch(FilamentTurnstileFacade::getResetEventName()); parent::throwFailureValidationException(); } }
Then override the login() method in your PanelProvider (for example, AdminPanelProvider):
namespace App\Providers\Filament; use App\Filament\Auth\Login; use Filament\Panel; use Filament\PanelProvider; class AdminPanelProvider extends PanelProvider { public function panel(Panel $panel): Panel { return $panel ->default() ->id('admin') ->path('admin') ->login(Login::class); // override the login page 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.
l3aro/filament-turnstile 适用场景与选型建议
l3aro/filament-turnstile 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.01k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2025 年 11 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「filament」 「l3aro」 「cloudflare-turnstile」 「filament-turnstile」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 l3aro/filament-turnstile 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 l3aro/filament-turnstile 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 l3aro/filament-turnstile 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A query database collection for use with Laravel Pipeline
Rating star for filament table column and schemas.
Alfabank REST API integration
Database-driven onboarding for Filament: progress checklists and guided spotlight tours, translatable to any locale.
Generate Table of Contents from HTML for PHP
Secure token-based authentication for first-party Laravel APIs
统计信息
- 总下载量: 10.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-11