coderflex / laravel-turnstile
一个帮助您实现Cloudflareturnstile "CAPTCHA替代方案"的软件包
Requires
- php: ^8.1|^8.2
- guzzlehttp/guzzle: ^7.7
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
Laravel Turnstile,是一个帮助您轻松且快速实现cloudflare turnstile的软件包。
安装
您可以通过composer安装此软件包
composer require coderflex/laravel-turnstile
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="turnstile-config"
这是发布配置文件的内容
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.', ], ];
可选地,您可以使用以下命令发布视图
php artisan vendor:publish --tag="turnstile-views"
turnstile密钥
要使用Cloudflare Turnstile,您需要从您的Cloudflare仪表板获取SiteKey
和SecretKey
在生成密钥后,请在您的.env
文件中使用TURNSTILE_SITE_KEY
和TURNSTILE_SECRET_KEY
TURNSTILE_SITE_KEY=2x00000000000000000000AB TURNSTILE_SECRET_KEY=2x0000000000000000000000000000000AA
如果您想测试小部件,可以使用Cloudflare提供的虚拟站点密钥和密钥
用法
turnstile小部件组件
一旦您需要此软件包,您可以在表单中使用turnstile小部件,如下所示
<x-turnstile-widget theme="dark" language="en-US" size="normal" callback="callbackFunction" errorCallback="errorCallbackFunction" />
如您所见,小部件有一些可用的选项。您可以在配置部分中了解更多信息
turnstile后端验证
一旦您在前端使用小部件组件,您可以使用validate
方法验证Cloudflare
响应。
以下是一个示例
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 } }
您还可以选择使用验证方法发送Cloudflare
响应。例如以下内容
public function store(Request $request) { ... $response = LaravelTurnstile::validate( $request->get('cf-turnstile-response'); // this will be created from the cloudflare widget. ); ... }
turnstile自定义规则
如果您想要干净验证,可以使用与表单验证一起使用的TurnstileCheck
自定义规则。以下是一个示例
use Coderflex\LaravelTurnstile\Rules\TurnstileCheck; public function store(Request $request) { $request->validate([ 'cf-turnstile-response' => [new TurnstileCheck()] ]); }
自定义规则将使用与后端验证
相同的逻辑,但将检查响应,并在验证失败时返回验证消息。
您可以在config/turnstile.php
文件中更改验证消息的内容
return [ ... 'error_messages' => [ 'turnstile_check_message' => 'The CAPTCHA thinks you are a robot! Please refresh and try again.', ], ];
PS:如果您想翻译消息,只需复制消息并翻译即可,因为它使用了背后的翻译方法。
真实案例示例
在您的blade文件中
<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>
在您的控制器中
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. }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志
贡献
有关详细信息,请参阅贡献指南
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略
致谢
许可协议
MIT许可(MIT)。有关更多信息,请参阅许可文件