djunehor/laravel-put-helper
Laravel middleware/helper that makes PUT request payloads easy to access, including uploaded files. Once installed, PUT input and files are available like normal request data, with support for validating file parameters using put_file.
Installation:
composer require djunehor/laravel-put-helper
config/app.php:
Djunehor\PutHelper\PutHelperServiceProvider::class
bootstrap/app.php:
$app->register(Djunehor\PutHelper\PutHelperServiceProvider::class);
First Use Case:
enctype="multipart/form-data").$name = $request->input('name'); // Works for strings
$file = $request->file('avatar'); // Use alternative methods (see below)
Middleware Integration:
The package adds a global middleware that parses raw input from PUT requests and merges it into the $request object. No manual parsing is required.
Accessing Inputs:
$request->input('key') or $request->key (standard Laravel methods).$request->file('key'). Instead:
$file = $request->file_key; // Alternative 1
$file = $request['file_key']; // Alternative 2
Validation:
Use the custom put_file rule to validate file uploads:
$request->validate([
'avatar' => 'required|put_file',
'name' => 'required|string',
]);
Frontend Integration:
method="PUT" and enctype="multipart/form-data".Testing: Simulate PUT requests in tests:
$response = $this->put('/endpoint', [
'name' => 'Test',
'avatar' => UploadedFile::fake()->image('avatar.jpg'),
]);
File Access Limitation:
$request->file('key') does not work. Use $request->key or $request['key'] instead.// ❌ Won't work:
$file = $request->file('avatar');
// ✅ Works:
$file = $request->avatar;
Middleware Order:
$request data (e.g., ConvertEmptyStringsToNull).Large Payloads:
Lumen Compatibility:
Verify Middleware:
Check if the middleware is registered by inspecting php artisan package:discover or manually verifying app/Http/Kernel.php.
Check Raw Input: Debug the raw input stream to ensure the package is parsing correctly:
$rawInput = file_get_contents('php://input');
dd($rawInput); // Verify payload structure
Validation Errors:
If put_file validation fails unexpectedly, ensure:
Custom Middleware: Override the package’s middleware behavior by publishing its config (if available) or extending the service provider:
// app/Providers/PutHelperServiceProvider.php
namespace App\Providers;
use Djunehor\PutHelper\PutHelperServiceProvider as BaseProvider;
class PutHelperServiceProvider extends BaseProvider {
public function register() {
parent::register();
// Custom logic here
}
}
File Handling:
Extend file parsing logic by modifying the package’s PutHelper class (located in vendor/djunehor/laravel-put-helper/src/PutHelper.php).
Testing: Mock the middleware in tests to isolate behavior:
$this->app->make(Djunehor\PutHelper\PutHelperMiddleware::class)->handle($request);
How can I help you explore Laravel packages today?