fernet/captcha

该包已被放弃且不再维护。未建议替代包。

Fernet 的 Google No CAPTCHA reCAPTCHA 插件

v0.1 2021-01-31 15:02 UTC

This package is auto-updated.

Last update: 2024-04-06 22:01:41 UTC


README

Google reCAPTCHA 插件用于 Fernet 框架。该插件使用了 No CAPTCHA reCAPTCHA 包。

配置

创建一个 Google reCAPTCHA v2 网站。将配置添加到 .env 文件中。

CAPTCHA_SITE="paste the site code"
CAPTCHA_SECRET="paste de secret code"

如果您手动配置了插件,别忘了将 "fernet/captcha" 添加到 plugins.json 文件中。

使用方法

该插件包含 2 个组件:和 . CaptchaScript 需要添加 Google 的脚本。

<?php
namespace App\Component;

use CaptchaFernet\Captcha;
use Symfony\Component\HttpFoundation\Request;

class TodoForm 
{
    private Captcha $captcha;
    private string $message = 'Press the submit button';

    public function __construct(Captcha $captcha)
    {
        $this->captcha = $captcha;
    }
    
    public function handleSubmit(Request $request)
    {
        if (!$this->captcha->verify($request)) {
            $this->message = 'Captcha ok!';
        } else {
            $this->message = 'Error Captcha';
        }
    }

    public function __toString(): string
    {
        return <<<EOH
            <h1>{$this->message}</h1>
            <form @onSubmit="handleSubmit">
                <Captcha />
                <button>Submit</button>
            </form>
EOH;
    }
}

别忘了添加脚本文件。

<?php
namespace App\Component;

class App 
{
    public $preventWrapper = true;

    public function __toString(): string
    {
        return <<<EOH
            <html>
            <body>
                <TodoForm />
                <CaptchaScript />
            </body>
            </html>
EOH;
    }
}