<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class RouterListener
{
public const ALLOWED_METHOD = [
Request::METHOD_PUT,
Request::METHOD_DELETE,
Request::METHOD_PATCH,
];
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
// don't do anything if it's not the main request
return;
}
$method = $event->getRequest()?->getMethod();
if ($method === Request::METHOD_GET) {
// don't do anything if it's method GET
return;
}
$realMethod = $event->getRequest()?->headers?->get('x-real-method');
if (!$realMethod) {
// don't do anything if it's not have custom header x-real-method
return;
}
if (!in_array($realMethod, self::ALLOWED_METHOD)) {
return;
}
$event->getRequest()->setMethod($realMethod);
}
}