coderflex/laravel-turnstile
Composer 安装命令:
composer require coderflex/laravel-turnstile
包简介
A package to help you implement the Cloudflare turnstile "CAPTCHA Alternative"
README 文档
README
Laravel Turnstile, is a package to help you implement cloudflare turnstile easily, and with no time.
Installation
You can install the package via composer:
composer require coderflex/laravel-turnstile
You can publish the config file with:
php artisan vendor:publish --tag="turnstile-config"
This is the contents of the published config file:
return [ /* |-------------------------------------------------------------------------- | Turnstile Keys |-------------------------------------------------------------------------- | | This value is the site, and the secret key of your application, after creating an application | with Cloudflare turnstile, copy the site key, and use it here, or in the .env | file. | Note that the secret key should not be publicly accessible. | | @see: https://developers.cloudflare.com/turnstile/get-started/#get-a-sitekey-and-secret-key | */ 'turnstile_site_key' => env('TURNSTILE_SITE_KEY', null), 'turnstile_secret_key' => env('TURNSTILE_SECRET_KEY', null), /* |-------------------------------------------------------------------------- | Error Messages |-------------------------------------------------------------------------- | | Here you can find the error messages for the application. You can modify | or translate the error message as you like. | | Note that you can translate the error message directly, without wrapping | them in translate helper. | */ 'error_messages' => [ 'turnstile_check_message' => 'The CAPTCHA thinks you are a robot! Please refresh and try again.', ], ];
Optionally, you can publish the views using:
php artisan vendor:publish --tag="turnstile-views"
Turnstile Keys
To be able to use Cloudflare Turnstile, you need to get the SiteKey, and the SecretKey from your Cloudflare dashboard
After Generating the keys, use TURNSTILE_SITE_KEY, and TURNSTILE_SECRET_KEY in your .env file
TURNSTILE_SITE_KEY=2x00000000000000000000AB TURNSTILE_SECRET_KEY=2x0000000000000000000000000000000AA
If you want to test the widget, you can use the Dummy site keys and secret keys that Cloudflare provides.
Usage
Turnstile Widget Component
Once you require this package, you can use the turnstile widget in your form, like the following
<x-turnstile-widget theme="dark" language="en-US" size="normal" callback="callbackFunction" errorCallback="errorCallbackFunction" />
As you can see, the widget has few options to use. You can know more about them in the configuration section
Turnstile Backend Validation
Once you used the widget component, in the frontend. You can validate Cloudflare Response, by using the validate method.
Here's an example:
use Coderflex\LaravelTurnstile\Facades\LaravelTurnstile; public function store(Request $request) { // maybe you want to validate your form first $response = LaravelTurnstile::validate(); if (! $response['success']) { // will return boolean // do your logic } }
You may, optionally, send the Cloudflare response with the validation method. Something like the following:
public function store(Request $request) { ... $response = LaravelTurnstile::validate( $request->get('cf-turnstile-response'); // this will be created from the cloudflare widget. ); ... }
Turnstile Custom Rule
If you want clean validation, you can use the TurnstileCheck custom rule, along with your form validation. Here's an example:
use Coderflex\LaravelTurnstile\Rules\TurnstileCheck; public function store(Request $request) { $request->validate([ 'cf-turnstile-response' => [new TurnstileCheck()] ]); }
The custom rule, will use the same logic, as the backend validation, but instead will check for the response, and return a validation message, if the captcha fails.
You can change the content of the validation message, in config/turnstile.php file
return [ ... 'error_messages' => [ 'turnstile_check_message' => 'The CAPTCHA thinks you are a robot! Please refresh and try again.', ], ];
PS: If you want to translate the message, just copy the message and translate it, because it uses the translator method behind the scene.
Real Life Example
In your blade file
<form action="" method="post"> @csrf <div> <input type="text" name="name" /> @error('name') <p class="error">{{ $message }}</p> @enderror </div> <div> <x-turnstile-widget theme="auto" language="fr"/> @error('cf-turnstile-response') <p class="error">{{ $message }}</p> @enderror </div> <button>Submit</button> </form>
In your controller:
use Coderflex\LaravelTurnstile\Rules\TurnstileCheck; use Coderflex\LaravelTurnstile\Facades\LaravelTurnstile; ... public function store(Request $request) { $request->validate([ 'name' => ['required', 'string', 'max:250'], 'cf-turnstile-response' => ['required', new TurnstileCheck()], ]); // or $response = LaravelTurnstile::validate(); if (! $response['success']) { // do your thing. } // do your things. }
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 the License File for more information.
coderflex/laravel-turnstile 适用场景与选型建议
coderflex/laravel-turnstile 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 387.88k 次下载、GitHub Stars 达 114, 最近一次更新时间为 2023 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「cloudflare」 「coderflex」 「turnstile」 「laravel-turnstile」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 coderflex/laravel-turnstile 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coderflex/laravel-turnstile 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 coderflex/laravel-turnstile 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Filament Plugin to help you implement Cloudflare Turnstile
Write Clean/Reusable Code with Presenters.
A package to keep track of your pages & understand your audience
Laravel Ticket System, to help you manage your tickets eaisly
Symfony bundle for Cloudflare integration: trusted proxies, real client IP detection, forwarded headers.
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 387.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 114
- 点击次数: 26
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-27