sf2/sf3 symfony cors headers php

Allow your symfony (sf2, sf3) to handle by headers, add config to services and create one.

services.yml

tool.cors:
    class: Lib\Services\Tool\CorsListener
    tags:
        - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

CorsListener.php

<?php


namespace Lib\Services\Tool;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;


class CorsListener
{
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $responseHeaders = $event->getResponse()->headers;

        $responseHeaders->set('Access-Control-Allow-Headers', 'origin, content-type, accept,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
        $responseHeaders->set('Access-Control-Allow-Origin', '*');
        $responseHeaders->set('Access-Control-Allow-Credentials','true' );
        $responseHeaders->set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, PATCH, OPTIONS');
        $responseHeaders->set('Allow', 'POST, GET, PUT, DELETE, PATCH, OPTIONS');
    }
}