buzz/laravel-google-captcha

Laravel 的 Google 验证码

v2.3.6 2024-02-29 14:40 UTC

This package is auto-updated.

Last update: 2024-08-29 15:46:50 UTC


README

支持页面上的多验证码

Google captcha for Laravel

anhskohbo/no-captcha 启发,基于 google captcha sdk 构建。

功能

  • 支持 Laravel 5/6/7/8/9/10/11

  • 页面上的多个验证码

  • 重置验证码

  • 自动发现服务提供者

  • 自定义请求方法

  • 使用不同的密钥

  • 运行时动态选项

安装

将以下行添加到 composer.json 文件的 require 部分

{
    "require": {
        "buzz/laravel-google-captcha": "2.*"
    }
}

或者

使用 composer 安装此包

composer require buzz/laravel-google-captcha

使用 composer update 更新包或使用 composer install 安装。

设置

支持 Laravel >=5.5 的自动发现

将 ServiceProvider 添加到 config/app.php 文件的 providers 数组中。

'Buzz\LaravelGoogleCaptcha\CaptchaServiceProvider',

发布配置

php artisan vendor:publish --provider="Buzz\LaravelGoogleCaptcha\CaptchaServiceProvider"

自定义 ReCaptcha 请求(最小版本 2.1.7)

编辑 config/captcha.php 配置文件中的 request_method

文件 config/captcha.php

<?php
/*
 * Secret key and Site key get on https://www.google.com/recaptcha
 * */
return [
    'secret' => env('CAPTCHA_SECRET', 'default_secret'),
    'sitekey' => env('CAPTCHA_SITEKEY', 'default_sitekey'),
    /**
     * @var string|null Default ``null``.
     * Custom with function name (example customRequestCaptcha) or class@method (example \App\CustomRequestCaptcha@custom).
     * Function must be return instance, read more in repo ``https://github.com/thinhbuzz/laravel-google-captcha-examples``
     */
    'request_method' => null,
    'options' => [
        'multiple' => false,
        'lang' => app()->getLocale(),
    ],
    'attributes' => [
        'theme' => 'light'
    ],
];

文件 app/helpers.php

<?php

function customRequestCaptcha(){
    return new \ReCaptcha\RequestMethod\Post();
}

或文件 app/CustomRequestCaptcha.php

<?php

namespace App;

class CustomRequestCaptcha
{
    public function custom()
    {
        return new \ReCaptcha\RequestMethod\Post();
    }
}

配置

CAPTCHA_SECRETCAPTCHA_SITEKEY 添加到 .env 文件

CAPTCHA_SECRET=[secret-key]
CAPTCHA_SITEKEY=[site-key]

使用

查看示例

examples 仓库 中获取示例

显示 reCAPTCHA

{!! app('captcha')->display($attributes) !!}

或使用 Facade:将 'Captcha' => '\Buzz\LaravelGoogleCaptcha\CaptchaFacade', 添加到 config/app.php 文件的 aliases 数组中,并在模板中使用

{!! Captcha::display($attributes) !!}

或使用表单

{!! Form::captcha($attributes) !!}

支持自定义语言

{!! app('captcha')->display($attributes = [], $options = ['lang'=> 'vi']) !!}

使用

// element attributes
$attributes = [
    'data-theme' => 'dark',
    'data-type' => 'audio',
];
// package options
$options = [
    'data-theme' => 'dark',
    'data-type'	=> 'audio',
];

有关更多信息,请参阅 google recaptcha 文档

请帮我为这个内容写一个 README

验证

'g-recaptcha-response' => 'required|captcha' 添加到规则数组。

use Validator;
use Illuminate\Support\Facades\Input;

$validate = Validator::make(Input::all(), [
    'g-recaptcha-response' => 'required|captcha'
]);

测试

当使用 Laravel 测试功能时,您需要模拟 captcha 表单元素的响应。对于涉及 captcha 的任何表单测试,您都可以模拟 facade 的行为

// Prevent validation error on captcha
        CaptchaFacade::shouldReceive('verify')
            ->andReturn(true);
            
// Provide hidden input for your 'required' validation
        CaptchaFacade::shouldReceive('display')
            ->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');
            
// Add these when testing multiple captchas on a single page
        CaptchaFacade::shouldReceive('displayJs');
        CaptchaFacade::shouldReceive('displayMultiple');
        CaptchaFacade::shouldReceive('multiple');

贡献

https://github.com/thinhbuzz/laravel-google-captcha/pulls