src/Infrastructure/EventSubscriber/AppCorsSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class AppCorsSubscriber implements EventSubscriberInterface
  10. {
  11.     public function onKernelRequest(RequestEvent $event): void
  12.     {
  13.         if (!$event->isMainRequest()) {
  14.             return;
  15.         }
  16.         if (Request::METHOD_PUT Request::METHOD_PATCH Request::METHOD_DELETE) {
  17.             $request $event->getRequest();
  18.             if ($request->server->has('BROWSER-ID')) {
  19.                 $request->headers->set('BROWSER-ID'$request->server->get('BROWSER-ID'));
  20.             }
  21.         }
  22.         if (Request::METHOD_OPTIONS === $event->getRequest()->getRealMethod()) {
  23.             $event->setResponse(new Response());
  24.         }
  25.     }
  26.     public function onKernelResponse(ResponseEvent $event)
  27.     {
  28.         $response $event->getResponse();
  29.         $response->headers->set('Access-Control-Allow-Origin''*');
  30.         $response->headers->set('Access-Control-Allow-Headers''*');
  31.         $response->headers->set('Access-Control-Allow-Methods''*');
  32.         $event->setResponse($response);
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             KernelEvents::REQUEST => ['onKernelRequest'9999],
  38.             KernelEvents::RESPONSE => ['onKernelResponse'1],
  39.         ];
  40.     }
  41. }