ntimes/route-time-limits 问题修复 & 功能扩展

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

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

ntimes/route-time-limits

Composer 安装命令:

composer require ntimes/route-time-limits

包简介

A Laravel package for limiting time spent on routes

README 文档

README

Latest Version on Packagist Total Downloads License

A Laravel package that allows you to set time limits on routes and track usage times based on user authentication status. This package helps you monitor and control how much time is spent on specific routes over a defined period, with different limits for authenticated and guest users.

📥 Installation

Install the package via composer:

composer require ntimes/route-time-limits

Publish the configuration and migrations:

php artisan vendor:publish --provider="NTimes\RouteTimeLimits\RouteTimeLimitsServiceProvider"

Run the migrations:

php artisan migrate

📚 Usage

🔰 Basic Usage

Add the middleware to your routes:

// In your routes file
Route::get('/dashboard', 'DashboardController@index')
    ->name('dashboard')
    ->middleware('route.time.limit');

// Or for route groups
Route::middleware('route.time.limit')->group(function () {
    Route::get('/admin', 'AdminController@index')->name('admin.index');
    Route::get('/admin/users', 'AdminController@users')->name('admin.users');
});

⚙️ Configuration

You can customize the package behavior in the config/route_time_limits.php file:

return [
    // Default maximum time allowed for all routes (in seconds)
    'default_max_time' => [
        'guest' => 300, // 5 minutes for guests
        'authenticated' => 600, // 10 minutes for authenticated users
    ],

    // How many days of data to keep in the database
    'data_retention_days' => 1,

    // Custom route configurations
    // Format: 'route_name' => ['guest' => time_in_seconds, 'authenticated' => time_in_seconds]
    'custom_routes' => [
        'admin.dashboard' => [
            'guest' => 0, // No access for guests
            'authenticated' => 900, // 15 minutes for authenticated users
        ],
        'api.users.index' => [
            'guest' => 60, // 1 minute for guests
            'authenticated' => 300, // 5 minutes for authenticated users
        ],
    ],
    
    // Whether to enable the middleware globally
    'enabled' => true,
    
    // Routes to exclude from time tracking
    'excluded_routes' => [
        'login',
        'register',
    ],
    
    // Whether to track by IP address for guest users
    'track_guest_by_ip' => true,
    
    // Identify user by this field (default: id)
    'user_identifier' => 'id',
];

🛠️ Available Commands

🧹 Cleanup Old Records

The package includes a command to clean up old records:

# Clean up all records
php artisan route-time-limits:cleanup

# Clean up only guest records
php artisan route-time-limits:cleanup --user-type=guest

# Clean up only authenticated user records
php artisan route-time-limits:cleanup --user-type=authenticated

This command is automatically scheduled to run daily, but you can also run it manually.

🔄 Reset Time Limits

For testing or administrative purposes, you can reset time limits:

# Reset all time limits
php artisan route-time-limits:reset --all

# Reset time limits for a specific route
php artisan route-time-limits:reset --route=admin.dashboard

# Reset time limits for a specific user
php artisan route-time-limits:reset --user-id=1

🔍 How It Works

flowchart TD
    A[Route Access] --> B{Check Limits}
    B --> C{Record Exists?}
    C -->|Yes| D{Time Limit Exceeded?}
    C -->|No| E[Create Record]
    D -->|Yes| F[Return 429]
    D -->|No| G[Track Time]
    E --> G
    G --> H[Continue Request]
Loading
  1. When a user visits a route with the middleware applied, the package checks if there's an existing record for that route and user type (guest or authenticated).
  2. If no record exists, it creates one with the default time limit (or a custom one if configured).
  3. Each time the route is accessed, the time spent is tracked and added to the daily total.
  4. If the total time exceeds the configured limit, a 429 (Too Many Requests) response is returned.
  5. The time counter is reset daily.
  6. Different time limits are applied based on whether the user is authenticated or a guest.
  7. Guest users can be tracked by IP address if configured.

📃 License

The MIT License (MIT). Please see License File for more information.

ntimes/route-time-limits 适用场景与选型建议

ntimes/route-time-limits 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「route」 「time」 「middleware」 「laravel」 「time-tracking」 「access-control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 ntimes/route-time-limits 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-27