首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

发布日期 经过

Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

温德尔·阿德里尔 发布 Laravel Idempotency , a package that adds HTTP idempotency to write-oriented Laravel routes. When a POST , PUT , 或者 PATCH request is retried with the same idempotency key and the same payload, the package replays the original cached response instead of running the route handler again — a common requirement for payment endpoints, order creation, and any API where clients may retry on network failure.

Applying the Middleware

The package provides two ways to attach idempotency to a route. The first is a standard route middleware:

使用 WendellAdriel\Idempotency\Http\Middleware\Idempotent ;
路线 :: 邮政 '/orders' , StoreOrderController ::班级 -> 中间件 同样强大 ::班级 (英文):

The middleware expects an Idempotency-Key header. When the same key is sent again with identical request data, the original response is returned with an Idempotency-Replayed: true header added.

Per-route configuration is available via Idempotent::using() :

路线 :: 邮政 '/payments' , ChargePaymentController ::班级 -> 中间件
同样强大 :: 使用
生存时间 : 600 ,
lockTimeout : 30 ,
必需的 : 错误的 ,
范围 : \WendellAdriel\Idempotency\Enums\IdempotencyScope :: Ip ,
标题 : 'X-Idempotency-Key' ,
(英文):

The second option is a PHP attribute, which works at the class or method level and accepts the same options:

使用 WendellAdriel\Idempotency\Attributes\Idempotent ;
使用 WendellAdriel\Idempotency\Enums\IdempotencyScope ;
#[ 同样强大 ]
班级 支付控制器
{
#[ 同样强大 生存时间 : 600 , lockTimeout : 30 , 范围 : IdempotencyScope :: Ip )]
民众 功能 店铺 ()
{
// ...
}
}

Since the attribute extends Laravel's built-in controller middleware attribute, onlyexcept work as expected.

Key Scoping

Idempotency keys can be scoped three ways, configured globally in config/idempotency.php or overridden per route:

范围 行为
user Keys are segmented by authenticated user; guest requests fall back to client IP
ip Keys are segmented by client IP address
global The same key applies across all users and IP addresses

Conflict Detection

The package handles two conflict scenarios. If a request arrives with the same key but a different payload, it returns 422 Unprocessable Entity . If a second matching request arrives while the first is still being processed — a true in-flight duplicate — it returns 409 ConflictRetry-After: 1 header. Both behaviors work through Laravel's cache atomic locks, so a cache driver with lock support (Redis, Memcached) is required.

工匠指令

Two commands let you inspect and clear cached entries without touching the cache directly.

idempotency:list renders a table of active entries with scope, identifier, key, route, status code, and expiry:

php 工匠 idempotency:list --scope=user --id=5

idempotency:forget removes entries by scope, identifier, or key. Destructive calls prompt for confirmation unless --force is passed:

# remove all entries for a specific user
php 工匠 idempotency:forget --scope=user --id=5 - 力量
# remove every entry for a given client-provided key
php 工匠 idempotency:forget --key=checkout-1 - 力量

You can find Laravel Idempotency on GitHub

保罗·雷德蒙德照片

Laravel News 特约撰稿人。全栈 Web 开发人员兼作家。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 代码审查

几天内即可获得 Laravel 代码审查方面的专家指导

访问 Laravel 代码审查
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
MongoDB 徽标

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
Laravel Web Push Notifications image

Laravel Web Push Notifications

阅读文章
RedBerry to Host Georgia's First Laravel Meetup in Tbilisi image

RedBerry to Host Georgia's First Laravel Meetup in Tbilisi

阅读文章
Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

阅读文章
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

阅读文章
Polyscope for Windows is Now Available image

Polyscope for Windows is Now Available

阅读文章
Laravel Sluggable image

Laravel Sluggable

阅读文章