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],
];
Enable JSON Parsing
Add the middleware to your kernel (e.g., app/Http/Kernel.php):
protected $middleware = [
// ...
\BFDsf\JsonRequestBundle\Middleware\JsonRequestMiddleware::class,
];
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!
// ...
}
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
}
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();
}
Validation
Integrate with Laravel’s Validator:
$validated = Validator::make($request->all(), [
'name' => 'required|string',
])->validate();
JsonRequestMiddleware is registered before validation middleware.$this->app->bind(\BFDsf\JsonRequestBundle\Parser\JsonParserInterface::class, CustomJsonParser::class);
$request->isJson() to test both JSON and non-JSON routes.Middleware Order
Place JsonRequestMiddleware before any middleware that reads raw request content (e.g., ConvertEmptyStringsToNullMiddleware).
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');
}
}
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);
JsonRequestMiddleware is registered via php artisan route:list (Symfony) or php artisan route:list --show-uri (Laravel).$request->all() in a try-catch:
try {
$data = $request->all();
} catch (\JsonException $e) {
Log::error("JSON parse error: " . $e->getMessage());
}
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));
}
}
Request Attribute Add parsed JSON to the request object:
$request->attributes->set('json', $request->all());
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
);
}
How can I help you explore Laravel packages today?