larowka / prevent-duplicate-requests
Prevent duplicate requests middleware for Laravel applications.
Requires
- php: ^8.1
- illuminate/http: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.16
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-11-21 15:45:30 UTC
README
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.
Installation
You can install the package via Composer:
composer require larowka/prevent-duplicate-requests
Usage
Laravel 11
Global Middleware
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); });
Assigning Middleware to Routes
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);
Middleware Aliases
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');
Laravel 10
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, ];
Features
- 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.
Testing
Run the tests using PHPUnit:
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security issues, please email larowka@icloud.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see LICENSE for more information.