daily-ci-api/src/App/Middleware/PreFlightMiddleware.php
2017-03-22 16:01:34 +01:00

35 lines
1.0 KiB
PHP

<?php
namespace App\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class PreFlightMiddleware
{
const ALLOW_HEADERS = [
'DNT',
'X-CustomHeader',
'Keep-Alive',
'User-Agent',
'X-Requested-With',
'If-Modified-Since',
'Cache-Control',
'Content-Type',
'Authorization',
];
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$requestMethod = strtoupper($request->getMethod());
if ($requestMethod == 'OPTIONS') {
return $response
->withHeader('Accept', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
->withHeader('Access-Control-Allow-Headers', implode(",", self::ALLOW_HEADERS));
}
return $next($request, $response);
}
}