nishit/laravel-idempotent
Composer 安装命令:
composer require nishit/laravel-idempotent
包简介
Laravel middleware to make POST, PUT, PATCH requests idempotent.
README 文档
README
Laravel Idempotent is a lightweight middleware package to make POST, PUT, and PATCH requests idempotent.
It prevents duplicate processing (e.g., double orders, repeated payments, or accidental form resubmissions).
Features
- Automatic generation of idempotency keys based on request path + payload.
- Optional manual idempotency via
Idempotency-Keyheader. - Configurable modes:
replay– return cached response for duplicate requestsblock– return HTTP status 409 or configured status
- Cache-based storage (supports
cacheorredis). - Route-specific middleware; no need to apply globally.
- Logging support for debugging duplicate requests.
Installation
Install via Composer:
composer require nishit/laravel-idempotent
Publish the configuration file:
php artisan vendor:publish --provider="Nishit\LaravelIdempotent\LaravelIdempotentServiceProvider" --tag=config
Configuration
Edit the published config/idempotent.php:
return [ 'ttl' => 3600, // Cache duration in seconds 'storage' => 'cache', // 'cache' or 'redis' 'duplicate_response' => 409, // HTTP status for duplicates in block mode 'enable_logging' => env('APP_DEBUG', false), 'mode' => 'replay', // 'replay' or 'block' ];
| Key | Description |
|---|---|
ttl |
How long the request response will be cached. |
storage |
Choose your cache driver (cache or redis). |
duplicate_response |
HTTP status returned for duplicates in block mode. |
enable_logging |
Log duplicate requests for debugging. |
mode |
replay returns cached response, block returns an error. |
Usage
Apply middleware to selected routes:
use App\Http\Controllers\OrderController; use Illuminate\Support\Facades\Route; use Illuminate\Http\Request; // Single route Route::post('/submit', function (\Illuminate\Http\Request $request) { return response()->json(['message' => 'Processed', 'data' => $request->all()]); })->middleware('idempotent'); Route::post('/order', [OrderController::class, 'create'])->middleware('idempotent'); // Or group routes Route::middleware(['idempotent'])->group(function () { Route::post('/order', [OrderController::class, 'create']); });
Only routes using the middleware will have idempotency applied. Other routes remain unaffected.
How It Works
-
Middleware generates a unique key per request using:
- Request path
- Request payload (POST / PUT / PATCH)
- Optional
Idempotency-Keyheader
-
If the key exists in cache:
- Replay mode → returns cached response
- Block mode → returns HTTP 409 or configured status
-
If the key does not exist, the request is processed and the response is cached for future duplicates.
Testing
Use curl to test idempotency:
# First request curl -X POST http://127.0.0.1:8000/api/submit \ -H "Content-Type: application/json" \ -d '{"name":"John","email":"john@example.com"}' # Repeat request with same payload or manual Idempotency-Key header curl -X POST http://127.0.0.1:8000/api/submit \ -H "Content-Type: application/json" \ -H "Idempotency-Key: 123456" \ -d '{"name":"John","email":"john@example.com"}'
- In replay mode, the second request returns the cached response.
- In block mode, the second request returns HTTP
409(or the configured status).
Logging
Enable logging in config/idempotent.php:
'enable_logging' => true,
Duplicate requests will be logged in storage/logs/laravel.log:
[2026-04-07 09:24:00] local.INFO: duplicate found {"key":"idempotent_<hash>"}
License
This package is open-sourced software licensed under the MIT License.
nishit/laravel-idempotent 适用场景与选型建议
nishit/laravel-idempotent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nishit/laravel-idempotent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nishit/laravel-idempotent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 41
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-07