From 66e244dcc1711da3ec4bb1c1dd022a62136fa15c Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 25 Feb 2020 10:30:06 +0300 Subject: [PATCH] feat(core): try to use native slim cors for api's --- flextype/bootstrap.php | 19 +++++++++++++++++++ flextype/middlewares.php | 7 ------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/flextype/bootstrap.php b/flextype/bootstrap.php index f3ee0105..3261f270 100755 --- a/flextype/bootstrap.php +++ b/flextype/bootstrap.php @@ -177,6 +177,18 @@ foreach ($shortcodes_extensions as $shortcodes_extension) { } } +$app->options('/{routes:.+}', function ($request, $response, $args) { + return $response; +}); + +$app->add(function ($req, $res, $next) { + $response = $next($req, $res); + return $response + ->withHeader('Access-Control-Allow-Origin', '*') + ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') + ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); +}); + /** * Init plugins */ @@ -187,6 +199,13 @@ $flextype['plugins']->init($flextype, $app); */ $flextype['themes']->init($flextype, $app); +// Catch-all route to serve a 404 Not Found page if none of the routes match +// NOTE: make sure this route is defined last +$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) { + $handler = $this->notFoundHandler; // handle using the default Slim page not found handler + return $handler($req, $res); +}); + /** * Run application */ diff --git a/flextype/middlewares.php b/flextype/middlewares.php index 3d5f0f2a..fd2887af 100644 --- a/flextype/middlewares.php +++ b/flextype/middlewares.php @@ -9,14 +9,7 @@ declare(strict_types=1); namespace Flextype; -use Tuupola\Middleware\CorsMiddleware; - /** * Add middleware CSRF (cross-site request forgery) protection for all routes */ $app->add($flextype->get('csrf')); - -/** - * Add middleware CorsMiddleware for Cross-origin resource sharing. - */ -$app->add(new CorsMiddleware);