写了个 Laravel 幂等组件

Laravel-Idempotent

由来

上周业务部门反应有重复创建订单的情况……
然后找了几个包看了看发现写的都不太完整,加上还没写过 Composer 包,醉醉醉重要的是买了超哥的教程一直在吃灰心里充满罪恶感;于是便有了这个包~

感兴趣的兄弟可以看看~~

安装

$ composer require lidongyooo/laravel-idempotent -vvv

配置

  1. config/app.php 注册 ServiceProvider (Laravel 5.5 + 无需手动注册)
'providers' => [
    // ...
    Lidongyooo\Idempotent\IdempotentServiceProvider::class,
],
  1. 创建配置文件
$ php artisan vendor:publish --tag="laravel-idempotent"
  1. 查看应用根目录下的 config/idempotent.php

使用

中间件 Lidongyooo\Idempotent\IdempotentMiddleware,别名 idempotent

// ...
Route::post('/test', function () {
    return 'test';
})->middleware('idempotent');

//如果你想强制缓存响应
Route::post('/test', function () {
    return 'test';
})->middleware('idempotent:true');

或者你可以将它加入到指定路由中间件组中

protected $middlewareGroups = [
    // ...
    'api' => [
        'idempotent',
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

返回值

  • 重复请求将会返回异常
abort(425, 'Your request is still being processed.');
  • 已缓存的请求将添加响应头
$response->header($this->config['back_header_name'], $idempotentKey);

更多详细介绍请查看 配置文件

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3

同买了超哥的教程,我当时就想把dingo-api的转换层单独拿出来,就自己整了一个转换层 github.com/bukeliyuchenmou/respons...

3年前 评论

原理是什么,redis吗,redis除了栈其他不是原子性的吧

3年前 评论
lidongyoo (楼主) 3年前
Asuna (作者) 3年前
lidongyoo (楼主) 3年前
lidongyoo (楼主) 3年前
lidongyoo (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!