genealabs/laravel-caffeine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

genealabs/laravel-caffeine

Composer 安装命令:

composer require genealabs/laravel-caffeine

包简介

Keeping Your Laravel Forms Awake

README 文档

README

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

Caffeine for Laravel masthead image.

🗂️ Table of Contents

📖 Summary

Prevent forms from timing out when submitting them after leaving them on-screen for a considerable amount of time. Laravel defaults session lifetime to 120 minutes, but that is configurable and could be different site-by-site.

☕ Caffeine works by sending a "drip" — a lightweight AJAX request at regular intervals — to keep the session alive while a form is open. It only activates on pages with a _token field or a csrf-token meta tag, so all other pages time-out as normal.

🔒 Why This Approach?

This package keeps the integrity of your site's security by avoiding the following:

  • 🚫 Exposing the CSRF Token on an unsecured endpoint.
  • 🚫 Eliminating CSRF Token validation on specific routes, or altogether.
  • 🚫 Removing session-timeout on all pages.

📋 Requirements

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

📦 Installation

composer require genealabs/laravel-caffeine

✨ The service provider is auto-discovered. No additional setup is required.

⚙️ Configuration

Only publish the config file if you need to customize it:

php artisan caffeine:publish --config

This creates the following config file:

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,

];

🚀 Usage

That's it! It will apply itself automatically where it finds a form with a _token field, or a meta tag named "csrf-token", while pages are open in browsers. 🎉

🚫 Prevent Caffeination

There are two methods to prevent Caffeine from keeping the session alive:

🏷️ Meta Tag Method

Add the following meta tag to any page you want to exclude:

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

🛣️ Route Middleware Method

Publish the config file and set use-route-middleware to true. This disables the default global middleware mode. Then selectively enable Caffeine on specific routes or route groups:

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

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

📝 Note: This will only have effect if the page includes a form. If not, the page will not caffeinate your application anyway.

⚠️ Considerations

🔌 Livewire / Inertia / SPA

This package works by injecting JavaScript that pings a keep-alive endpoint. It is designed for traditional Blade forms. If you are using Livewire or Inertia, their built-in request cycles typically keep the session alive already, so this package is generally unnecessary in those contexts.

🚧 Incompatible Packages

  • Voyager has been reported as being incompatible. To work around this, configure Caffeine to use route-based middleware on all non-Voyager routes.

🛤️ Routes

This package registers routes under genealabs/laravel-caffeine.

⬆️ Upgrading

0.6.0

This update changed the config file setting names. Delete the published config file config/genealabs-laravel-caffeine.php if it exists, and re-publish using the command in the Configuration section.

For all other version changes, see the Releases page on GitHub.

🤝 Contributing

Contributions are welcome! 🎉 Please review the Contribution Guidelines and observe the Code of Conduct before submitting a pull request.

🧪 Quality Checklist

  • ✅ Achieve as close to 100% code coverage as possible using unit tests.
  • ✅ Be fully PSR-1, PSR-4, and PSR-12 compliant.
  • ✅ Provide an up-to-date CHANGELOG.md adhering to Keep a Changelog.
  • ✅ Have no PHPMD or PHPCS warnings throughout all code.

🔐 Security

If you discover a security vulnerability, please report it via GitHub Security Advisories rather than opening a public issue.

Built with ❤️ for the Laravel community using lots of ☕ by Mike Bronner.

This is an MIT-licensed open-source project. Its continued development is made possible by the community. If you find it useful, please consider 💖 becoming a sponsor and ⭐ starring it on GitHub.

genealabs/laravel-caffeine 适用场景与选型建议

genealabs/laravel-caffeine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.13M 次下载、GitHub Stars 达 937, 最近一次更新时间为 2015 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 genealabs/laravel-caffeine 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.13M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 942
  • 点击次数: 9
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 937
  • Watchers: 23
  • Forks: 86
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-14