Hi there!
I have a question. How do you manage CORS in Slim PHP?
I’m reading the documentation about setting up CORS:
https://www.slimframework.com/docs/v4/cookbook/enable-cors.html
The following code should enable CORS:
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
});
$app->add(function ($request, $handler) {
$response = $handler->handle($request);
return $response
->withHeader('Access-Control-Allow-Origin', 'http://mysite')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
});
With for example:
// For one specific domain.
->withHeader('Access-Control-Allow-Origin', 'http://mysite.com')
// For any domain.
->withHeader('Access-Control-Allow-Origin', '*')
I’m wondering, what if I want to enable multiple domains? (but just my trustworthy domains, not all of them with ‘*’)
I tried with domains separated by commas, but I get an error like this:
(Running PHP 8.0.6 Built-In Web Server)
UPDATE:
While I’m writing this, I’m thinking of maintaining a list of available domains for CORS.
And if $_SERVER[‘HTTP_ORIGIN’] exists in this array of available domains, enable CORS for this specific domain.
(I think it should work, but maybe you know others way more simple -without using $_SERVER[‘HTTP_ORIGIN’]- )
Any thoughts?
Thank you in advance!