承接 webmavens/auto-google-recaptcha 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webmavens/auto-google-recaptcha

Composer 安装命令:

composer require webmavens/auto-google-recaptcha

包简介

Globally add Google reCAPTCHA to all the forms and validate it.

README 文档

README

A package to add Google reCAPTCHA v2 Invisible / v3 protection to your entire application forms. Supports Laravel (all versions) and Core PHP projects.

🚀 Installation

Require the package via Composer:

composer require webmavens/auto-google-recaptcha

⚙️ Laravel Setup

Laravel 11 and above

No manual registration needed — Service Provider and Facade are auto-discovered.

Middleware

In Laravel 11+, add the middleware to the web group in your bootstrap/app.php file:

->withMiddleware(function (Middleware $middleware) {
    $middleware->group('web', [
        \WebMavens\AutoGoogleRecaptcha\Laravel\Middleware\VerifyRecaptcha::class,
    ]);
})

Laravel 10 and below

For older Laravel versions, you need to register service provider, facade, and middleware manually.

Service Provider

Add to config/app.php providers array:

'providers' => [
    // ...
    WebMavens\AutoGoogleRecaptcha\AutoGoogleRecaptchaServiceProvider::class,
],

Facade

Add to config/app.php aliases array:

'aliases' => [
    // ...
    'AutoReCaptcha' => WebMavens\AutoGoogleRecaptcha\Laravel\Facades\AutoGoogleRecaptcha::class,
],

Middleware

Add to app/Http/Kernel.php in the web middleware group:

protected $middlewareGroups = [
    'web' => [
        // ...
        \WebMavens\AutoGoogleRecaptcha\Laravel\Middleware\VerifyRecaptcha::class,
    ],
];

Publish Config & Assets

For all Laravel versions, publish the config and JS file:

php artisan vendor:publish --provider="WebMavens\AutoGoogleRecaptcha\Laravel\AutoGoogleRecaptchaServiceProvider"

This publishes:

  • Config → config/auto-google-recaptcha.php
  • JS → public/vendor/auto-google-recaptcha/auto-recaptcha.js

Note: You can then make changes into javascript or config file based on your needs. Also you can add the javascript validation and specify when you want to load the reCAPTCHA by adding or modifying javascript.

Environment Variables

Get the sitekey and secret from Google Recaptcha. Add the following to your .env file,

NOCAPTCHA_SITEKEY=your-site-key
NOCAPTCHA_SECRET=your-secret-key
NOCAPTCHA_ENABLE=true

Render JS

Add this to your main layout (e.g. layouts/app.blade.php):

{!! AutoReCaptcha::renderJs() !!}

This ensures reCAPTCHA script is injected globally.

⚙️ Core PHP Setup

You can also use this package in plain PHP projects.

  1. Copy Middleware.php from the package src/ directory to your php project's root directory.
  2. In your index.php (or main entry file), include it as this (Middleware.php file handles validations for each requests based on the configuration.):
<?php

require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/Middleware.php';

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>reCAPTCHA Test</title>
    <!-- Load Google + package JS -->
    <?= $captcha->renderJs() ?>
</head>
<body>
    <h1>Test reCAPTCHA Form</h1>
    <form method="POST" action="">
        <input type="text" name="name" placeholder="Your Name" required>
        <button type="submit">Submit</button>
    </form>
</body>
</html>
  1. Add your config in config/auto-google-recaptcha.php (same format as Laravel).

🔧 Config

The published config file looks like this:

return [
    'secret' => env('NOCAPTCHA_SECRET', ''),
    'sitekey' => env('NOCAPTCHA_SITEKEY', ''),
    'options' => [
        'timeout' => 30,

        // Methods requiring captcha
        'allowed_methods' => [
            'POST',
            'PUT',
            'DELETE'
        ],

        // Routes to exclude from captcha
        // Note: For Laravel, Add route name with wildcards (eg. post.create, post.edit, post.*) 
        // Note: For PHP, Add url paths to exclude with wildcards (eg. /post/create, /post/edit, *post*)
        'excluded_routes' => [
            'admin.*' // supports wildcards
        ],

        // Enable/disable globally
        'enable' => env('NOCAPTCHA_ENABLE', true),
    ],
];

Other Config & Notes

  • For core PHP project replace env() function with static values if you haven't downloaded env package, else it will throw function error.

  • If you don't want to add the reCAPTCHA to any form in frontend then add data-no-captcha attribute to form tag and it will exclude that form from rendering the reCAPTCHA. You can use this feature with allowed_methods config to completely exclude any form from reCAPTCHA validation.

Ajax Config

  • This prebuilt JS also add captcha to all ajax requests.

  • If you don't want to add the reCAPTCHA to any ajax request in frontend then add disableCaptcha: true attribute to ajax options and it will exclude that request from adding the reCAPTCHA. You can use this feature with allowed_methods config to completely exclude any ajax request from reCAPTCHA validation.

Manual reCAPTCHA

  • You can also add google reCAPTCHA to any form manually if needed:
  1. Create a div with data-size="invisible".
<div class="g-recaptcha"
    data-sitekey="_your_site_key_"
    data-callback="onSubmit"
    data-size="invisible">
</div>
  1. Call grecaptcha.execute from a javascript method.
grecaptcha.execute();

Read Full Documentation for reCAPTCHA v2 here and for reCAPTCHA v3 here

🛡️ Behavior

  • Captcha enforced only on configured HTTP methods (default: POST, PUT, DELETE)
  • Skips validation on excluded routes (admin.*, etc.)
  • On failure → returns HTTP 403
  • Validation uses Google reCAPTCHA v2 Invisible or v3 API

✨ Features

  • Works with Laravel 11+ (auto-discovery) and older versions
  • Works with Core PHP projects
  • Global middleware validation
  • Configurable methods and route exclusions
  • Supports reCAPTCHA v2 Invisible & v3
  • Easy asset publishing

📄 License

MIT License © Web Mavens

webmavens/auto-google-recaptcha 适用场景与选型建议

webmavens/auto-google-recaptcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 webmavens/auto-google-recaptcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webmavens/auto-google-recaptcha 我们能提供哪些服务?
定制开发 / 二次开发

基于 webmavens/auto-google-recaptcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-02