diff --git a/public/index.php b/public/index.php index eb91c15..9f22a34 100644 --- a/public/index.php +++ b/public/index.php @@ -1,16 +1,18 @@ handle($request); (new SapiEmitter())->emit($response); \ No newline at end of file diff --git a/src/Cors.php b/src/Cors.php new file mode 100644 index 0000000..03c98d9 --- /dev/null +++ b/src/Cors.php @@ -0,0 +1,21 @@ + '*', + 'Access-Control-Allow-Headers' => '*', + 'Access-Control-Allow-Methods' => '*', + 'Access-Control-Max-Age' => 86400, + ]); + } +} diff --git a/src/Router.php b/src/Router.php index e2e2feb..d9b77e1 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,38 +1,39 @@ get('/', Index::class); $r->get('/test', Test::class); $r->post('/questions', Questions::class); $r->put('/answers', Answers::class); + $r->addRoute(['OPTIONS'], '/{any}', Cors::class); }); $route = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); switch($route[0]) { case \FastRoute\Dispatcher::FOUND: /** @var MiddlewareInterface $method */ $method = $route[1]; $request = $request->withAttribute('Method', $method); return $handler->handle($request); default: case \FastRoute\Dispatcher::NOT_FOUND: return new JsonResponse(['error' => 'Not found'], 404); case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED: return new JsonResponse(['error' => 'Method not allowed'], 405, ['Allow' => implode(', ', $route[1])]); } } }