Add Context::setCorsPolicy() method #2287

This commit is contained in:
Kijin Sung 2024-03-24 23:07:21 +09:00
parent eb5dd18659
commit ff41a14237

View file

@ -473,6 +473,32 @@ class Context
}
}
/**
* Set CORS policy for the current request.
*
* @param string $origin Origin to allow, or '*'
* @param array $methods
* @param array $headers
* @param int $max_age
* @return void
*/
public static function setCorsPolicy(string $origin = '*', array $methods = [], array $headers = [], int $max_age = 0): void
{
header('Access-Control-Allow-Origin: ' . $origin);
if (count($methods))
{
header('Access-Control-Allow-Methods: ' . implode(', ', $methods));
}
if (count($headers))
{
header('Access-Control-Allow-Headers: ' . implode(', ', $headers));
}
if ($max_age > 0)
{
header('Access-Control-Max-Age: ' . $max_age);
}
}
/**
* Load the database information
*