dutchcodingcompany/livewire-recaptcha
Composer 安装命令:
composer require dutchcodingcompany/livewire-recaptcha
包简介
Add Google Recaptcha V3 support to your Laravel Livewire components
README 文档
README
This package provides a custom Livewire directive to protect your Livewire functions with a Google reCAPTCHA (v2 + v2 invisible + v3) check.
Installation
composer require dutchcodingcompany/livewire-recaptcha
Configuration
Read https://developers.google.com/recaptcha/intro on how to create your own key pair for the specific ReCaptcha version you are going to implement.
This package supports the following versions. Note that each version requires a different sitekey/secretkey pair:
| Version | Docs | Notes |
|---|---|---|
| v3 (recommended) | V3 Docs | |
| v3 (enterprise) | V3 Docs | Use 'version' => 'v3-enterprise' |
| v2 | V2 Docs | |
| v2 invisible | V2 Docs | Use 'size' => 'invisible' |
Your options should reside in the config/services.php file:
// V3 config: 'google' => [ 'recaptcha' => [ 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'), 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'), 'version' => 'v3', 'score' => 0.5, // An integer between 0 and 1, that indicates the minimum score to pass the Captcha challenge. 'endpoint' => 'https://www.google.com/recaptcha/api/siteverify', // For enterprise users, fill in your URL from Google Console. ], ], // V2 config: 'google' => [ 'recaptcha' => [ 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'), 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_KEY'), 'version' => 'v2', 'size' => 'normal', // 'normal', 'compact' or 'invisible'. 'theme' => 'light', // 'light' or 'dark'. ], ],
Component
In your Livewire component, at your form submission method, add the #[ValidatesRecaptcha] attribute:
use Livewire\Component; use DutchCodingCompany\LivewireRecaptcha\ValidatesRecaptcha; class SomeComponent extends Component { // (optional) Set a response property on your component. // If not given, the `gRecaptchaResponse` property is dynamically assigned. public string $gRecaptchaResponse; #[ValidatesRecaptcha] public function save(): mixed { // Your logic here will only be called if the captcha passes... } }
For fine-grained control, you can pass a custom secret key and minimum score (applies only to V3) using:
#[ValidatesRecaptcha(secretKey: 'mysecretkey', score: 0.9)]
View
On the view side, you have to include the Blade directive @livewireRecaptcha. This adds two scripts to the page,
one for the reCAPTCHA script and one for the custom Livewire directive to hook into the form submission.
Preferrably these scripts are only added to the page that has the Captcha-protected form (alternatively, you can add
the @livewireRecaptcha directive on a higher level, lets say your layout).
Secondly, add the new directive wire:recaptcha to the form element that you want to protect.
<!-- some-livewire-component.blade.php --> <!-- (optional) Add error handling --> @if($errors->has('gRecaptchaResponse')) <div class="alert alert-danger">{{ $errors->first('gRecaptchaResponse') }}</div> @endif <!-- Add the `wire:recaptcha` Livewire directive --> <form wire:submit="save" wire:recaptcha> <!-- The rest of your form --> <!-- When using version 2, insert the placeholder element --> <div id="g-recaptcha-element"></div> <button type="submit">Submit</button> </form> <!-- Add the `@livewireRecaptcha` Blade directive --> @livewireRecaptcha
You can override any of the configuration values using:
@livewireRecaptcha(
version: 'v2',
siteKey: 'abcd_efgh-hijk_LMNOP',
theme: 'dark',
size: 'compact',
)
Finishing up
The Google ReCAPTCHA validation will automatically occur before the actual form is submitted. Before the save() method
is executed, a serverside request will be sent to Google to verify the Captcha challenge. Once the reCAPTCHA
response has been successful, your actual Livewire component method will be executed.
Error handling
When an error occurs with the Captcha validation, a ValidationException is thrown for the key gRecaptchaResponse.
There is a translatable error message available under 'livewire-recaptcha::recaptcha.invalid_response'.
dutchcodingcompany/livewire-recaptcha 适用场景与选型建议
dutchcodingcompany/livewire-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.39k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2024 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Google ReCaptcha」 「livewire」 「dutchcodingcompany」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dutchcodingcompany/livewire-recaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dutchcodingcompany/livewire-recaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dutchcodingcompany/livewire-recaptcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A simple and easy to use Google ReCaptcha v3 package for Laravel
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
ReCaptcha form field integration for Symfony
统计信息
- 总下载量: 32.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-06