CORS on Slim v4 at slim4-skeleton

Thanks to for GitHub - odan/slim4-skeleton: A Slim 4 Skeleton. It’s useful for startup.

I found Middleware/CorsMiddleware.php, but I don’t know how to use it .

I add ’ $app->add(CorsMiddleware::class);’ to config/middleware.php , but it’s not work for me.

Is any idea for it?

Thank you.

config/middleware.php

return function (App $app) {
// Parse json, form data and xml
$app->addBodyParsingMiddleware();

    $app->add(ValidationExceptionMiddleware::class);
    $app->add(TwigMiddleware::class);
    $app->add(TranslatorMiddleware::class);
    $app->add(SessionMiddleware::class);
    $app->add(CorsMiddleware::class);
    $app->addRoutingMiddleware();
    $app->add(BasePathMiddleware::class);
    $app->add(ErrorMiddleware::class);
};

Hi @huthief

Can you please show us the browser error message
and the request + response headers?

Read more: https://odan.github.io/2019/11/24/slim4-cors.html

Hi, @odan

There are the infomation .
The frontend is angular. Maybe is angular issue?

Access to XMLHttpRequest at ‘https://www.fycd.info/api/auth/v1/login’ from origin ‘https://cds04.fycd.info’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

  1. The Access-Control-Request-Headers header is missing in the request. This could be an Angular XHR config issue.

  2. Add the routes for the http OPTIONS method to handle the preflight request. See my link from above.

1 Like

@odan Thank you. It works.

I missed the routes for the http OPTIONS method to handle the preflight request XD