You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.3 KiB

3 years ago
  1. # Stack/Cors
  2. Library and middleware enabling cross-origin resource sharing for your
  3. http-{foundation,kernel} using application. It attempts to implement the
  4. [W3C Recommendation] for cross-origin resource sharing.
  5. [W3C Recommendation]: http://www.w3.org/TR/cors/
  6. Build status: ![.github/workflows/run-tests.yml](https://github.com/asm89/stack-cors/workflows/.github/workflows/run-tests.yml/badge.svg)
  7. ## Installation
  8. Require `asm89/stack-cors` using composer.
  9. ## Usage
  10. This package can be used as a library or as [stack middleware].
  11. [stack middleware]: http://stackphp.com/
  12. ### Options
  13. | Option | Description | Default value |
  14. |------------------------|------------------------------------------------------------|---------------|
  15. | allowedMethods | Matches the request method. | `array()` |
  16. | allowedOrigins | Matches the request origin. | `array()` |
  17. | allowedOriginsPatterns | Matches the request origin with `preg_match`. | `array()` |
  18. | allowedHeaders | Sets the Access-Control-Allow-Headers response header. | `array()` |
  19. | exposedHeaders | Sets the Access-Control-Expose-Headers response header. | `false` |
  20. | maxAge | Sets the Access-Control-Max-Age response header. | `false` |
  21. | supportsCredentials | Sets the Access-Control-Allow-Credentials header. | `false` |
  22. The _allowedMethods_ and _allowedHeaders_ options are case-insensitive.
  23. You don't need to provide both _allowedOrigins_ and _allowedOriginsPatterns_. If one of the strings passed matches, it is considered a valid origin.
  24. If `array('*')` is provided to _allowedMethods_, _allowedOrigins_ or _allowedHeaders_ all methods / origins / headers are allowed.
  25. ### Example: using the library
  26. ```php
  27. <?php
  28. use Asm89\Stack\CorsService;
  29. $cors = new CorsService(array(
  30. 'allowedHeaders' => array('x-allowed-header', 'x-other-allowed-header'),
  31. 'allowedMethods' => array('DELETE', 'GET', 'POST', 'PUT'),
  32. 'allowedOrigins' => array('http://localhost'),
  33. 'allowedOriginsPatterns' => array('/localhost:\d/'),
  34. 'exposedHeaders' => false,
  35. 'maxAge' => false,
  36. 'supportsCredentials' => false,
  37. ));
  38. $cors->addActualRequestHeaders(Response $response, $origin);
  39. $cors->handlePreflightRequest(Request $request);
  40. $cors->isActualRequestAllowed(Request $request);
  41. $cors->isCorsRequest(Request $request);
  42. $cors->isPreflightRequest(Request $request);
  43. ```
  44. ## Example: using the stack middleware
  45. ```php
  46. <?php
  47. use Asm89\Stack\Cors;
  48. $app = new Cors($app, array(
  49. // you can use array('*') to allow any headers
  50. 'allowedHeaders' => array('x-allowed-header', 'x-other-allowed-header'),
  51. // you can use array('*') to allow any methods
  52. 'allowedMethods' => array('DELETE', 'GET', 'POST', 'PUT'),
  53. // you can use array('*') to allow requests from any origin
  54. 'allowedOrigins' => array('localhost'),
  55. // you can enter regexes that are matched to the origin request header
  56. 'allowedOriginsPatterns' => array('/localhost:\d/'),
  57. 'exposedHeaders' => false,
  58. 'maxAge' => false,
  59. 'supportsCredentials' => false,
  60. ));
  61. ```