PHP code example of chenpkg / laravel-idempotent

1. Go to this page and download the library: Download chenpkg/laravel-idempotent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chenpkg / laravel-idempotent example snippets


Route::group(['middleware' => 'idempotent'], function () {
    //...
});

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

// config/idempotent.php

return [
    // true 自动获取唯一key, false 前端提供
    'forcible' => true,

    // 需要过滤重复请求的请求类型
    'methods' => ['POST', 'PUT', 'PATCH'],

    // 缓存有效时间/秒,防止死锁
    'seconds' => 10,

    // 获取当前用户
    'resolve_user' => function (\Illuminate\Http\Request $request) {
        return auth()->user();
    },

    // 前端提供 key 请求头名称.
    'header_name' => 'Idempotent-Key',
];
shell
$ php artisan vendor:publish --tag="laravel-idempotent"