rareloop / lumberjack-recaptcha

该软件包的最新版本(v1.0.1)没有提供许可证信息。

v1.0.1 2023-04-05 14:12 UTC

This package is auto-updated.

Last update: 2024-09-05 17:11:49 UTC


README

本软件包为Lumberjack和lumberjack-validation软件包提供了一个简单的ReCAPTCHA v2集成。

安装

composer require rareloop/lumberjack-recaptcha

安装后,在config/app.php中注册服务提供者

'providers' => [
    ...

    Rareloop\Lumberjack\Recaptcha\RecaptchaServiceProvider::class,

    ...
],

将示例config/recaptcha.php文件复制到您的主题目录。

将您的ReCAPTCHA凭证添加到您的.env文件

GOOGLE_RECAPTCHA_KEY="ABC123"
GOOGLE_RECAPTCHA_SECRET="ABC123"

# Optional, if you need to specify the hostname directly
GOOGLE_RECAPTCHA_HOSTNAME="my-hostname.com"

使用方法

确保在您希望添加支持的表单中调用recaptcha() Twig函数

<form method="" action="">
    <input name="my-input-name" type="text">
    {{ recaptcha() }}
    <button type-"submit">Submit</button>
</form>

然后添加验证到您的表单类中以确保其被检查

<?php

namespace App\Http\Forms;

use Rareloop\Lumberjack\Validation\AbstractForm;

class ContactForm extends AbstractForm
{
    /**
     * The validation rules for this form
     *
     * @type {Array}
     */
    protected $rules = [
        'my-input-name' => 'required',
        'g-recaptcha-response' => ['required', 'recaptcha'],
    ];
}

显示错误

如果您希望在某些原因导致ReCAPTCHA失败时显示错误,假设您将标准的errors数组传递到视图中,您可以简单地检查是否存在g-recaptcha-response

<form method="" action="">
    <input name="my-input-name" type="text">
    {{ recaptcha() }}
    {% if errors['g-recaptcha-response'] %}
        {% for error in errors['g-recaptcha-response'] %}
            <p class="error">{{ error }}</p>
        {% endfor %}
    {% endif %}
    <button type-"submit">Submit</button>
</form>