ITADN
mikebronner/laravel-caffeine
mikebronner/laravel-caffeine · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

☕ Laravel 的 Caffeine

GitHub Actions Scrutinizer GitHub (pre-)release Packagist GitHub license PHP Version Laravel GitHub Stars

Caffeine for Laravel masthead image.

🗂️ 目录

📖 摘要

防止表单在屏幕上停留相当长的时间后提交时超时。Laravel 默认会话生命周期为 120 分钟,但这是可配置的,并且可能因站点而异。

☕ Caffeine 通过定期发送“滴灌”——一种轻量级 AJAX 请求——来保持表单打开期间会话的活跃状态。它仅在包含 _token 字段或 csrf-token meta 标签的页面上激活,因此所有其他页面 均按正常方式超时。

🔒 为什么采用这种方式?

该包通过避免以下行为来保持网站的安全性:

  • 🚫 在不安全的端点上暴露 CSRF Token。
  • 🚫 在特定路由上或完全取消 CSRF Token 验证。
  • 🚫 移除所有页面的会话超时。

📋 要求

  • PHP 8.2+
  • Laravel 11, 12, or 13

📦 安装

composer require genealabs/laravel-caffeine

✨ 服务提供者会被自动发现。无需额外设置。

⚙️ 配置

仅在需要自定义时发布配置文件

php artisan caffeine:publish --config

这将创建以下配置文件:

return [
    /*
    |--------------------------------------------------------------------------
    | Drip Interval
    |--------------------------------------------------------------------------
    |
    | Here you may configure the interval with which Caffeine for Laravel
    | keeps the session alive. By default this is 5 minutes (expressed
    | in milliseconds). This needs to be shorter than your session
    | lifetime value configured set in "config/session.php".
    |
    | Default: 300000 (int)
    |
    */
    'drip-interval' => 300000,

    /*
    |--------------------------------------------------------------------------
    | Domain
    |--------------------------------------------------------------------------
    |
    | You may optionally configure a separate domain that you are running
    | Caffeine for Laravel on. This may be of interest if you have a
    | monitoring service that queries other apps. Setting this to
    | null will use the domain of the current application.
    |
    | Default: null (null|string)
    |
    */
    'domain' => null,

    /*
    |--------------------------------------------------------------------------
    | Drip Endpoint URL
    |--------------------------------------------------------------------------
    |
    | Sometimes you may wish to white-label your app and not expose the AJAX
    | request URLs as belonging to this package. To achieve that you can
    | rename the URL used for dripping caffeine into your application.
    |
    | Default: 'genealabs/laravel-caffeine/drip' (string)
    |
    */
    'route' => 'genealabs/laravel-caffeine/drip',

    /*
    |--------------------------------------------------------------------------
    | Checking for Lapsed Drips
    |--------------------------------------------------------------------------
    |
    | If the browser tab is suspended due to inactivity or the device is put to
    | sleep, it will still cause an error when trying to submit the form. To
    | avoid this, we force-reload the form 2 minutes prior to session
    | time-out or later. Setting this setting to 0 will disable this
    | check if you don't want to use it.
    |
    | Default: 2000 (int)
    |
    */
    'outdated-drip-check-interval' => 2000,

    /*
    |--------------------------------------------------------------------------
    | Use Route Middleware
    |--------------------------------------------------------------------------
    |
    | Drips are enabled via route middleware instead of global middleware.
    |
    | Default: false (bool)
    |
    */
    'use-route-middleware' => false,

];

🚀 用法

就这样!当页面在浏览器中打开时,它会自动应用于包含 _token 字段的表单,或名为 "csrf-token" 的 meta 标签。 🎉

🚫 防止 Caffeination

有两种方法可以防止 Caffeine 保持会话活跃:

🏷️ Meta 标签方法

在你想排除的任何页面中添加以下 meta 标签:

<meta name="caffeinated" content="false">

🛣️ 路由中间件方法

发布配置文件并将 use-route-middleware 设置为 true。这会禁用 默认的中间件全局模式。然后有选择地在特定 路由或路由组上启用 Caffeine:

Route::any('test', 'TestController@test')->middleware('caffeinated');

Route::middleware(['caffeinated'])->group(function () {
    Route::any('test', 'TestController@test');
});

📝 注意: 仅当页面包含表单时,此设置才会生效。否则, 该页面无论如何都不会使您的应用程序保持活跃。

⚠️ 注意事项

🔌 Livewire / Inertia / SPA

此包通过注入 JavaScript 来 ping 一个 keep-alive 端点。它 专为传统的 Blade 表单设计。如果您正在使用 LivewireInertia,它们内置的请求循环通常已经保持会话活跃, 因此在此类上下文中通常不需要此包。

🚧 不兼容的包

  • Voyager 已被报告为 不兼容。为了绕过此问题,请配置 Caffeine 在所有非 Voyager 路由上使用 基于路由的中间件。

🛤️ 路由

此包在 genealabs/laravel-caffeine 下注册路由。

⬆️ 升级

0.6.0

此更新更改了配置文件中的设置名称。如果存在已发布的配置 文件 config/genealabs-laravel-caffeine.php,请将其删除,并使用 Configuration 部分中的命令重新发布。

有关其他版本变更,请参阅 GitHub 上的 Releases 页面。

🤝 贡献

欢迎贡献!🎉 请在提交拉取请求之前审阅 Contribution Guidelines 并遵守 Code of Conduct

🧪 质量检查清单

  • ✅ 使用单元测试尽可能接近 100% 的代码覆盖率。
  • ✅ 完全符合 PSR-1、PSR-4 和 PSR-12 标准。
  • ✅ 提供最新的 CHANGELOG.md,并遵循 Keep a Changelog
  • ✅ 所有代码中没有任何 PHPMD 或 PHPCS 警告。

🔐 安全

如果您发现安全漏洞,请通过 GitHub Security Advisories 进行报告,而不是公开提交 issue。


使用大量 ☕ 由 Mike Bronner 为 Laravel 社区构建,充满 ❤️。

这是一个采用 MIT 许可证的开源项目。其持续发展得益于社区的支持。如果您觉得它有用,请考虑 💖 成为赞助者 以及 ⭐ 在 GitHub 上给它加星