laragear/expire-route 问题修复 & 功能扩展

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

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

laragear/expire-route

Composer 安装命令:

composer require laragear/expire-route

包简介

Don't find route models created after a moment in time.

README 文档

README

Latest Version on Packagist Latest stable test run Codecov Coverage Maintainability Sonarcloud Status Laravel Octane Compatibility

Never found models or objects past their expiration time.

use Illuminate\Support\Facades\Route;
use App\Models\Payment;
use App\Models\Party;

Route::get('/payment/{payment}', function (Payment $payment) {
    // ...
})->middleware('expires');

Become a sponsor

Your support allows me to keep this package free, up to date and maintainable. Alternatively, you can spread the word!

Requirements

  • PHP 8.3 or later
  • Laravel 12 or later

Installation

Just fire up Composer and require the package in your application

composer require laragear/expire-route

Usage

While Laravel Temporarily Protected Routes works great for making routes available for a given amount of time, this library uses your Eloquent Model in the route to expire it through a middleware.

To better understand how the middleware works, let's imagine we have the App\Models\Payment model with an expires_at attribute that determines when the payment should be become invalid. The expires middleware does this automatically: if the expires_at time is past, the request will be aborted with a HTTP 410 Gone code.

use Illuminate\Support\Facades\Route;
use App\Models\Payment;

Route::get('payment/{payment}', function (Payment $invite) {
    // ...
})->middleware('expires');

Multiple route parameters

By default, the middleware will always check for the last route parameter in a route. You may set the name of the parameter if you require to check its expiration time.

use Illuminate\Support\Facades\Route;
use App\Models\Payment;
use App\Models\Detail;

Route::get('payment/{payment}/detail/{detail}')
    ->uses(function (Payment $payment, Detail $detail) {
        // ...
    })
    ->middleware('expires:payment');

Custom attribute

If your model doesn't have an expires_at attribute to check, you can use dot.notation to traverse the object attributes and find the expiration time.

use Illuminate\Support\Facades\Route;
use App\Models\Payment;

Route::get('payment/{payment}', function (Payment $payment) {
    // ...
})->middleware('expires:payment.due_at');

Relative expiration

When you have a model that doesn't have an expiration time, you can set a time in minutes (or a string to be parsed by strtotime()) to calculate from the created_at attribute when the route should expire.

For example, by setting 60, the route will expire once 60 minutes have passed since the creation of the App\Models\Payment model.

use Illuminate\Support\Facades\Route;
use App\Models\Payment;

Route::get('payment/{payment}', function (Payment $party) {
    // ...
})->middleware('expires:payment,60');

If you want to calculate the time from other attribute than created_at, issue the name of the attribute using dot.notation.

use Illuminate\Support\Facades\Route;
use App\Models\Payment;

Route::get('payment/{payment}', function (Payment $party) {
    // ...
})->middleware('expires:payment.issued_at,24 hours');

Warning

If the property or attribute doesn't exist or returns null, it will be assumed the model has not expired yet.

Using the routeExpiresAt() method

If the model or object implements the Laragear\ExpireRoute\Contracts\RouteExpirable contract, the routeExpiresAt() method will be used to retrieve the moment the route it should expire.

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Laragear\ExpireRoute\Contracts\RouteExpirable;

class Payment extends Model implements RouteExpirable
{
    // ...
    
    public function routeExpiresAt(): DateTimeInterface
    {
        return $this->created_at->addMinutes(60);
    }
}

Important

Using the contract takes precedence, unless an attribute is specified by the middleware declaration itself.

Non Eloquent Models

Both middlewares are not limited to only Eloquent Models. It can be any object (even an array) that has a UNIX Epoch timestamp or a datetime, since the check is done by retrieving the value through data_get() and then parsed by Laravel's Date Factory.

use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Route;

class Thing
{
    public function __construct(public $expiredAt)
    {
        // 
    }
}

Route::bind('thing', fn($time = 'now') => new Thing($value));

Route::get('some/{thing}', function (Thing $thing) {
    // ...
})->middleware('expires:thing.expiredAt');

Fluent middleware declaration

You may also use the Laragear\ExpireRoute\Http\Middleware\Expires middleware to fluently configure it in your route. It's a great way to set relative time expressively.

use Illuminate\Support\Facades\Route;
use Laragear\ExpireRoute\Http\Middleware\Expires;

// Set the parameter name and attribute
Route::get('/payment/{payment}/details/{detail}')
    ->middleware(Expires::using('payment.expiration_time'));

// Set the relative amount of time to check.
Route::get('/payment/{payment}')
    ->middleware(Expires::in(1)->hour()->and(30)->minutes());

// Set the relative amount of time to check.
Route::get('/payment/{payment}')
    ->middleware(Expires::after('60 minutes');

Laravel Octane compatibility

  • There are no singletons using a stale application instance.
  • There are no singletons using a stale config instance.
  • There are no singletons using a stale request instance.
  • There are no static properties written during a request.

There should be no problems using this package with Laravel Octane.

Security

If you discover any security-related issues, please email darkghosthunter@gmail.com instead of using the issue tracker.

License

This specific package version is licensed under the terms of the MIT License, at the time of publishing.

Laravel is a Trademark of Taylor Otwell. Copyright © 2011–2026 Laravel LLC.

laragear/expire-route 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-27