I just wanted to remove CORS error from my React
application to my Slim Framework 4 API.
composer require zrnik/php-slim-cors
Requires Slim Framework 4 & PHP 8.1+
Add this middleware to your \Slim\App
:
$app = new \Slim\App();
$app->add(new \Zrnik\SlimCors\CorsMiddleware());
That's it. There are optional parameters for the CorsMiddleware
object:
$app = new \Slim\App();
$app->add(
new \Zrnik\SlimCors\CorsMiddleware(
allowedOrigins: [
'my-frontend.app',
'another-app.com',
],
allowedMethods: [
'GET', 'POST'
]
)
);