for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestBodyListener implements EventSubscriberInterface
{
public function onKernelController(FilterControllerEvent $event)
$request = $event->getRequest();
if ($request->getContentType() != 'json' || !$request->getContent()) {
return;
}
$data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new BadRequestHttpException('Invalid json body: ' . json_last_error_msg());
$request->request->replace(is_array($data) ? $data : []);
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
return [
KernelEvents::CONTROLLER => ['onKernelController', 1],
];