Weave Code
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

Json Request Bundle Laravel Package

bf-dsf/json-request-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require bf-dsf/json-request-bundle
    

    Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via bridge):

    return [
        // ...
        BFDsf\JsonRequestBundle\BFDsfJsonRequestBundle::class => ['all' => true],
    ];
    
  2. Enable JSON Parsing Add the middleware to your kernel (e.g., app/Http/Kernel.php):

    protected $middleware = [
        // ...
        \BFDsf\JsonRequestBundle\Middleware\JsonRequestMiddleware::class,
    ];
    
  3. First Use Case Replace manual json_decode($request->getContent()) with native $request->get():

    public function store(Request $request) {
        $name = $request->get('name'); // Now works for JSON requests!
        // ...
    }
    

Implementation Patterns

Workflows

  1. API Endpoints Use $request->get() or $request->all() for JSON payloads (e.g., from Angular/Vue):

    public function update(Request $request) {
        $data = $request->all(); // Parsed JSON as associative array
    }
    
  2. Form Data + JSON Hybrid The bundle auto-detects JSON content type (Content-Type: application/json). For mixed requests:

    if ($request->isJson()) {
        $jsonData = $request->all();
    } else {
        $formData = $request->query->all();
    }
    
  3. Validation Integrate with Laravel’s Validator:

    $validated = Validator::make($request->all(), [
        'name' => 'required|string',
    ])->validate();
    

Integration Tips

  • Symfony Bridge: If using Symfony components, ensure the bundle’s JsonRequestMiddleware is registered before validation middleware.
  • Custom Parsing: Override parsing logic via service binding:
    $this->app->bind(\BFDsf\JsonRequestBundle\Parser\JsonParserInterface::class, CustomJsonParser::class);
    
  • Testing: Mock $request->isJson() to test both JSON and non-JSON routes.

Gotchas and Tips

Pitfalls

  1. Middleware Order Place JsonRequestMiddleware before any middleware that reads raw request content (e.g., ConvertEmptyStringsToNullMiddleware).

  2. Content-Type Headers The bundle checks for application/json. For custom types (e.g., application/vnd.api+json), extend the parser:

    class CustomParser implements JsonParserInterface {
        public function parse($content) {
            return json_decode($content, true, 512, JSON_THROW_ON_ERROR);
        }
        public function supports($contentType) {
            return str_contains($contentType, 'json');
        }
    }
    
  3. Large Payloads Default json_decode may hit memory limits. Adjust json_decode flags or stream parsing:

    $data = json_decode($request->getContent(), true, 512, JSON_BIGINT_AS_STRING);
    

Debugging

  • Verify Middleware: Check if JsonRequestMiddleware is registered via php artisan route:list (Symfony) or php artisan route:list --show-uri (Laravel).
  • Log Parsing Errors: Wrap $request->all() in a try-catch:
    try {
        $data = $request->all();
    } catch (\JsonException $e) {
        Log::error("JSON parse error: " . $e->getMessage());
    }
    

Extension Points

  1. Custom Parsers Implement JsonParserInterface for non-standard JSON (e.g., NDJSON):

    class NdJsonParser implements JsonParserInterface {
        public function parse($content) {
            return array_map('json_decode', explode("\n", $content));
        }
    }
    
  2. Request Attribute Add parsed JSON to the request object:

    $request->attributes->set('json', $request->all());
    
  3. Laravel-Specific For Laravel, bind the parser in AppServiceProvider:

    public function register() {
        $this->app->bind(
            \BFDsf\JsonRequestBundle\Parser\JsonParserInterface::class,
            \BFDsf\JsonRequestBundle\Parser\LaravelJsonParser::class
        );
    }
    
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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours