Middleware for Laravel that prevents duplicate requests based on user actions.
This middleware is designed to manage and prevent duplicate requests within a specified timeframe, ensuring that only unique requests are processed.
You can install the package via Composer:
composer require larowka/prevent-duplicate-requests
To apply middleware globally to every HTTP request in Laravel 11, you can use the withMiddleware method in your bootstrap/app.php file:
use \Larowka\PreventDuplicateRequests\Middleware\PreventDuplicateRequests;
->withMiddleware(function ($middleware) {
// ...
$middleware->append(PreventDuplicateRequests::class);
});
If you want to assign middleware to specific routes, use the middleware method when defining the route:
Copy code
use \Larowka\PreventDuplicateRequests\Middleware\PreventDuplicateRequests;
Route::get('/example', function () {
// Route logic...
})->middleware(PreventDuplicateRequests::class);
You can define aliases for middleware in your bootstrap/app.php file to use shorter names for middleware classes:
use \Larowka\PreventDuplicateRequests\Middleware\PreventDuplicateRequests;
->withMiddleware(function ($middleware) {
$middleware->alias([
'preventDuplicate' => PreventDuplicateRequests::class,
]);
});
Once defined, you can use the alias when assigning middleware to routes:
Route::get('/example', function () {
// Route logic...
})->middleware('preventDuplicate');
Add the middleware to your Laravel application's HTTP kernel:
// app/Http/Kernel.php
protected $routeMiddleware = [
// ...
'preventDuplicate' => \Larowka\PreventDuplicateRequests\Middleware\PreventDuplicateRequests::class,
];
Apply the middleware to your routes:
Route::middleware('preventDuplicate')->get('/example', function () {
return 'Unique request handled.';
});
Or use globally:
// app/Http/Kernel.php
protected $middleware = [
// ...
\Larowka\PreventDuplicateRequests\Middleware\PreventDuplicateRequests::class,
];
- Prevents Duplicate Requests: Blocks duplicate requests within a specified timeframe.
- Flexible Configuration: Customize the duration for which requests are cached.
- Supports Authenticated Users: Differentiates between authenticated and unauthenticated users.
- Idempotency Support: Ensures idempotent actions are enforced for user-specific operations.
Run the tests using PHPUnit:
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see LICENSE for more information.