1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-15 17:44:17 +02:00

feat(core): try to use native slim cors for api's

This commit is contained in:
Awilum
2020-02-25 10:30:06 +03:00
parent fbecd6be06
commit 66e244dcc1
2 changed files with 19 additions and 7 deletions

View File

@@ -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
*/

View File

@@ -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);