for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Controlabs\Handler\Slim;
use Psr\Container\ContainerInterface;
use Slim\Http\Request;
use Slim\Http\Response;
class Cors
{
const HEADERS = [
'Content-Type',
'Accept',
'Origin',
'Authorization',
'X-Requested-With'
];
protected $container;
public function __construct(ContainerInterface $container)
$this->container = $container;
}
public function __invoke(Request $request, Response $response, $next)
$route = $request->getAttribute('route');
$methods = [];
if (empty($route)) {
// Methods holds all of the HTTP Verbs that a particular route handles.
$methods[] = $request->getMethod();
} else {
$pattern = $route->getPattern();
$router = $this->container->get('router');
foreach ($router->getRoutes() as $route) {
if ($pattern === $route->getPattern()) {
$methods = array_merge_recursive($methods, $route->getMethods());
if ($next && is_callable($next)) {
$response = $next($request, $response);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', implode(',', self::HEADERS))
->withHeader('Access-Control-Allow-Methods', implode(',', $methods));