Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Put Helper Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require djunehor/laravel-put-helper
    
    • For Laravel 5.4 or older, manually register the service provider in config/app.php:
      Djunehor\PutHelper\PutHelperServiceProvider::class
      
    • For Lumen, register in bootstrap/app.php:
      $app->register(Djunehor\PutHelper\PutHelperServiceProvider::class);
      
  2. First Use Case:

    • Send a PUT request with form data (e.g., via Postman or frontend form with enctype="multipart/form-data").
    • Access the input data in your controller:
      $name = $request->input('name'); // Works for strings
      $file = $request->file('avatar'); // Use alternative methods (see below)
      

Implementation Patterns

Core Workflow

  1. 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.

  2. Accessing Inputs:

    • Strings: Use $request->input('key') or $request->key (standard Laravel methods).
    • Files: Due to a limitation, avoid $request->file('key'). Instead:
      $file = $request->file_key; // Alternative 1
      $file = $request['file_key']; // Alternative 2
      
  3. Validation: Use the custom put_file rule to validate file uploads:

    $request->validate([
        'avatar' => 'required|put_file',
        'name'   => 'required|string',
    ]);
    
  4. Frontend Integration:

    • Ensure forms use method="PUT" and enctype="multipart/form-data".
    • For APIs, send raw JSON/XML in the request body (the package handles parsing).
  5. Testing: Simulate PUT requests in tests:

    $response = $this->put('/endpoint', [
        'name' => 'Test',
        'avatar' => UploadedFile::fake()->image('avatar.jpg'),
    ]);
    

Gotchas and Tips

Pitfalls

  1. File Access Limitation:

    • $request->file('key') does not work. Use $request->key or $request['key'] instead.
    • Example:
      // ❌ Won't work:
      $file = $request->file('avatar');
      
      // ✅ Works:
      $file = $request->avatar;
      
  2. Middleware Order:

    • Ensure the package’s middleware runs before other middleware that might override $request data (e.g., ConvertEmptyStringsToNull).
  3. Large Payloads:

    • The package reads raw input, which can cause memory issues for very large payloads. Test with edge cases.
  4. Lumen Compatibility:

    • Lumen requires manual registration (see Installation). Forgetting this will break PUT request handling.

Debugging Tips

  1. Verify Middleware: Check if the middleware is registered by inspecting php artisan package:discover or manually verifying app/Http/Kernel.php.

  2. 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
    
  3. Validation Errors: If put_file validation fails unexpectedly, ensure:

    • The file is included in the PUT request.
    • The field name matches exactly (case-sensitive).

Extension Points

  1. 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
        }
    }
    
  2. File Handling: Extend file parsing logic by modifying the package’s PutHelper class (located in vendor/djunehor/laravel-put-helper/src/PutHelper.php).

  3. Testing: Mock the middleware in tests to isolate behavior:

    $this->app->make(Djunehor\PutHelper\PutHelperMiddleware::class)->handle($request);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log
hsluv/hsluv
seampact/codegen
seampact/hooks
seampact/ir
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk