Можно создать 2 EventSubscriber'а
На запрос:
// ...
class RequestSubscriber implements EventSubscriberInterface {
public function onRequestEvent(RequestEvent $event) {
if($event->getRequest()->getMethod() === 'OPTIONS') {
$response = new Response(null);
$event->setResponse($response);
}
}
public static function getSubscribedEvents() {
return [RequestEvent::class => ['onRequestEvent', 5000]];
}
}
И на ответ:
// ...
class ResponseSubscriber implements EventSubscriberInterface {
public function onResponseEvent(ResponseEvent $event) {
$headers = $event->getResponse()->headers;
$headers->set('Access-Control-Allow-Origin', '*');
if($event->getRequest()->getMethod() === 'OPTIONS') {
$headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
$headers->set('Access-Control-Allow-Headers', 'Authorization, Content-Type, Accept');
$headers->set('Access-Control-Allow-Credentials', 'true');
$headers->set('Access-Control-Max-Age', '1728000');
$headers->set('Cache-Control', 'no-cache, must-revalidate');
}
}
public static function getSubscribedEvents() {
return [ResponseEvent::class => 'onResponseEvent'];
}
}