timehunter / laravel-google-recaptcha-v2
Laravel for Google reCAPTCHA v2 的包
Requires
- php: ^7.2.5 || ^8.0
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- orchestra/testbench: ^6.0
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpunit: ~9.0
This package is auto-updated.
Last update: 2024-09-12 09:39:21 UTC
README
Laravel for Google reCAPTCHA v2 的包
欢迎所有票证(功能/问题/错误/改进),我将在48小时内对所有票证做出回应。
这是一个用于 Google reCAPTCHA v2 的包。
如果您想使用 v3,请访问: https://github.com/RyanDaDeng/laravel-google-recaptcha-v3
DEMO
复选框
隐藏 - 隐藏
隐藏 - 内联
角落
描述
用于 Google reCAPTCHA v2 的 Laravel 包。
如果您想制作自己的前端模板,您可以完全访问修改模板文件,因此您可以通过阅读 Google 官方指南(无论是隐藏徽章还是内联复选框)来自定义自己的模板。 https://developers.google.com/recaptcha/docs/display
特性
- 支持隐藏、角落和内联徽章样式
- 支持同一页面上的多个 reCAPTCHA,用于不同的表单
- 支持同一页面上的多个操作
- 支持在配置界面上的自定义实现
- 支持在请求方法接口上的自定义实现
- 支持在模板文件上的自定义实现
要求
此包需要以下依赖项
-
Laravel 5.x
-
如果您想使用验证类,您的 Laravel 版本需要 >= 5.5
-
php > 5
-
请确保您已阅读有关 Google reCAPTCHA v2 的基本信息。
安装
通过 Composer
$ composer require timehunter/laravel-google-recaptcha-v2 "~1.0.0" -vvv
如果您的 Laravel 框架版本 <= 5.4,请在您的配置文件中注册服务提供者:/config/app.php,否则请跳过。
'providers'=[ ...., TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider::class ]
还有
'aliases'=[ ...., 'GoogleReCaptchaV2'=> TimeHunter\LaravelGoogleReCaptchaV2\Facades\GoogleReCaptchaV2::class ]
如果您的 Laravel 框架版本 >= 5.5,只需运行以下命令来发布配置。
$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider" --tag=googlerecaptchav2.config
可选:如果您想修改或自定义自己的模板,首先发布默认视图,然后更改配置文件中的 'template'。
$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider" --tag=googlerecaptchav2.views
安装后,您应该在 views 文件夹下看到一个 googlerecaptchav2/template.blade,并在您的 app/config 文件夹中有一个 googlerecaptchav2.php。
基本用法
在配置文件中设置您的 Google reCAPTCHA 详细信息
请在 host_name、site_key、secret_key 和 site_verify_url 上注册所有详细信息。
有关更多详细信息,请参阅配置文件中的注释。
显示 reCAPTCHA v2
注意:对于使用 reCAPTCHA v2 徽章进行样式设计,官方网站不支持。如果您想的话,您仍然可以在其 div 元素上自定义它。
Blade
在您的表单中包含一个 ID 的 div,例如:
<div id="form_id_1"></div> <div id="form_id_2"></div>
在您的页面的底部/页眉中包含模板脚本,例如:
{!! GoogleReCaptchaV2::render('form_id_1','form_id_2') !!}
示例用法
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}} <form method="POST" action="/verify"> @csrf <div id="form_1_id"></div> <input type="submit" value="submit"> </form> <form method="POST" action="/verify"> @csrf <div id="form_2_id"></div> <input type="submit" value="submit"> </form> {!! GoogleReCaptchaV2::render('form_1_id','form_2_id') !!}
后端请求将接收到 'g-recaptcha-response' 的值,请参阅示例用法部分和外观用法部分。
徽章显示
重要性:您始终可以制作自己的模板,只需在配置中分配您的模板即可
[ ... 'template' => 'test.template' // if your template is located at resources/views/test/template ... ]
复选框
- 转到配置文件,并设置:
[ ... 'badge' => 'inline' ... ]
- 徽章将以复选框格式在表单中显示。
隐藏 - 内联
- 设置为不可见大小
[ ... 'size' => 'invisible' ... ]
- 设置为内联或 bottomright 或 bottomleft
[ ... 'badge' => 'inline' // also support: bottomright,bottomleft ... ]
隐藏 - 隐藏
- 设置为不可见大小
[ ... 'size' => 'invisible' ... ]
- 修改您的 div,使其 style display:none
- 请参阅 Google 官方网站: https://developers.google.com/recaptcha/docs/faq ,您需要包含以下文本
This site is protected by reCAPTCHA and the Google <a href="https://policies.google.com/privacy">Privacy Policy</a> and <a href="https://policies.google.com/terms">Terms of Service</a> apply.
角落
- 设置为不可见大小
[ ... 'size' => 'invisible' ... ]
- 设置为 bottomright/bottomleft
[ ... 'badge' => 'bottomright' ... ]
验证类(仅支持 Laravel >= 5.5)
您可以使用提供的验证对象来验证您的 reCAPTCHA。
use TimeHunter\LaravelGoogleReCaptchaV2\Validations\GoogleReCaptchaV2ValidationRule $rule = [ 'g-recaptcha-response' => [new GoogleReCaptchaV2ValidationRule()] ];
外观用法
您也可以通过调用以下方法直接使用已注册的服务。
- verifyResponse() 方法接受来自您表单的令牌值。此方法返回 Google reCAPTCHA 响应对象。
GoogleReCaptchaV2::verifyResponse($value, $ip=null);
示例用法
GoogleReCaptchaV2::verifyResponse($value,$ip)->getMessage(); GoogleReCaptchaV2::verifyResponse($value)->isSuccess(); GoogleReCaptchaV2::verifyResponse($value)->toArray();
GoogleReCaptchaV2::verifyResponse($request->input('g-recaptcha-response'))->getMessage()
示例用例
-
在配置中注册您的操作,同时启用评分并设置您自己的站点密钥和密钥。
-
在 web.php 中注册两个路由。
Route::get('/index', 'ReCaptchaController@index'); Route::post('/verify', 'ReCaptchaController@verify');
- 在控制器中创建两个函数。
public function verify(Request $request) { dd(GoogleReCaptchaV2::verifyResponse($request->input('g-recaptcha-response'))->getMessage()); } public function index(Request $request) { return view('index'); }
- 在 index.blade.php 中创建您的表单。
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}} <form method="POST" action="/verify"> @csrf <div id="contact_us_id"></div> <input type="submit" value="submit"> </form> <form method="POST" action="/verify"> @csrf <div id="signup_id"></div> <input type="submit" value="submit"> </form> {!! GoogleReCaptchaV2::render('contact_us_id','signup_id') !!}
高级用法
模板上的自定义实现
发布视图后,在 googlerecaptchaV2 下创建了一个 blade 文件,您可以自定义它并更改配置文件中的模板值,例如,如果您的模板保存在 resources/views/test/template,则应将值设置为以下内容
[ ... 'template' => 'test.template' ... ]
配置上的自定义实现
对于一些用户,他们可能在自己的存储中存储配置详细信息,例如数据库。您可以创建自己的类并实现。
TimeHunter\LaravelGoogleReCaptchaV2\Interfaces\ReCaptchaConfigv2Interface
请记得在自己的服务提供商中注册。
$this->app->bind( ReCaptchaConfigV2Interface::class, YourOwnCustomImplementation::class );
请求方法上的自定义实现
该软件包有两个默认的验证选项:Guzzle 和 Curl。如果您想使用自己的请求方法,您可以创建自己的类并实现。
TimeHunter\LaravelGoogleReCaptchaV2\Interfaces\RequestClientInterface
请记得在自己的服务提供商中注册。
$this->app->bind( RequestClientInterface::class, YourOwnCustomImplementation::class );
贡献者
@markheramis
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 ryandadeng@gmail.com 而不是使用问题跟踪器。
许可证
MIT。有关更多信息,请参阅许可证文件。