obsidian/MyObsidianVault/WebDev/2 CodeIgniter/_Course Notes/25.) CORS (Cross Origin Resource Sharing).md
2024-12-08 09:19:03 +01:00

754 B

https://www.youtube.com/watch?v=8GhfHZlEaEo&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=41

In case Web Server throws CORS errors when trying to access API. We add a filter that adds header with CORS info to define which kind of HTTP are allowed from foreign source.

Create CorsFilter.php

php spark make:filter Cors --suffix

Add to before method:

public function before(RequestInterface $request, $arguments=null)
{
	header("Access-Control-Allow-Origin: *");
	header("Access-Control-Allow-Headers: Authorization, Content-Type, Accept, API-KEY");
	header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH");
}

Add in app/Config/Filters.php: ! !