|
| 1 | +# Stack/Cors |
| 2 | +## Stand-alone fork of https://github.com/asm89/stack-cors |
| 3 | + |
| 4 | +[](https://github.com/fruitcake/php-cors/actions/workflows/run-tests.yml) |
| 5 | +[](http://choosealicense.com/licenses/mit/) |
| 6 | +[](https://packagist.org/packages/fruitcake/php-cors) |
| 7 | +[](https://packagist.org/packages/fruitcake/php-cors) |
| 8 | +[](https://fruitcake.nl/) |
| 9 | + |
| 10 | +Library and middleware enabling cross-origin resource sharing for your |
| 11 | +http-{foundation,kernel} using application. It attempts to implement the |
| 12 | +[W3C Recommendation] for cross-origin resource sharing. |
| 13 | + |
| 14 | +[W3C Recommendation]: http://www.w3.org/TR/cors/ |
| 15 | + |
| 16 | +Build status:  |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Require `fruitcake/php-cors` using composer. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +This package can be used as a library. You can use it in your framework using: |
| 25 | + |
| 26 | + - [Stack middleware](http://stackphp.com/): https://github.com/asm89/stack-cors |
| 27 | + - [Laravel](https://laravel.com): https://github.com/fruitcake/laravel-cors |
| 28 | + |
| 29 | + |
| 30 | +### Options |
| 31 | + |
| 32 | +| Option | Description | Default value | |
| 33 | +|------------------------|------------------------------------------------------------|---------------| |
| 34 | +| allowedMethods | Matches the request method. | `[]` | |
| 35 | +| allowedOrigins | Matches the request origin. | `[]` | |
| 36 | +| allowedOriginsPatterns | Matches the request origin with `preg_match`. | `[]` | |
| 37 | +| allowedHeaders | Sets the Access-Control-Allow-Headers response header. | `[]` | |
| 38 | +| exposedHeaders | Sets the Access-Control-Expose-Headers response header. | `false` | |
| 39 | +| maxAge | Sets the Access-Control-Max-Age response header. | `false` | |
| 40 | +| supportsCredentials | Sets the Access-Control-Allow-Credentials header. | `false` | |
| 41 | + |
| 42 | +The _allowedMethods_ and _allowedHeaders_ options are case-insensitive. |
| 43 | + |
| 44 | +You don't need to provide both _allowedOrigins_ and _allowedOriginsPatterns_. If one of the strings passed matches, it is considered a valid origin. |
| 45 | + |
| 46 | +If `['*']` is provided to _allowedMethods_, _allowedOrigins_ or _allowedHeaders_ all methods / origins / headers are allowed. |
| 47 | + |
| 48 | +### Example: using the library |
| 49 | + |
| 50 | +```php |
| 51 | +<?php |
| 52 | + |
| 53 | +use Fruitcake\Cors\CorsService; |
| 54 | + |
| 55 | +$cors = new CorsService([ |
| 56 | + 'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'], |
| 57 | + 'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'], |
| 58 | + 'allowedOrigins' => ['http://localhost'], |
| 59 | + 'allowedOriginsPatterns' => ['/localhost:\d/'], |
| 60 | + 'exposedHeaders' => false, |
| 61 | + 'maxAge' => false, |
| 62 | + 'supportsCredentials' => false, |
| 63 | +]); |
| 64 | + |
| 65 | +$cors->addActualRequestHeaders(Response $response, $origin); |
| 66 | +$cors->handlePreflightRequest(Request $request); |
| 67 | +$cors->isActualRequestAllowed(Request $request); |
| 68 | +$cors->isCorsRequest(Request $request); |
| 69 | +$cors->isPreflightRequest(Request $request); |
| 70 | +``` |
| 71 | + |
| 72 | +## Example: using the stack middleware |
| 73 | + |
| 74 | +```php |
| 75 | +<?php |
| 76 | + |
| 77 | +use Fruitcake\Cors\Cors; |
| 78 | + |
| 79 | +$app = new Cors($app, [ |
| 80 | + // you can use ['*'] to allow any headers |
| 81 | + 'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'], |
| 82 | + // you can use ['*'] to allow any methods |
| 83 | + 'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'], |
| 84 | + // you can use ['*'] to allow requests from any origin |
| 85 | + 'allowedOrigins' => ['localhost'], |
| 86 | + // you can enter regexes that are matched to the origin request header |
| 87 | + 'allowedOriginsPatterns' => ['/localhost:\d/'], |
| 88 | + 'exposedHeaders' => false, |
| 89 | + 'maxAge' => false, |
| 90 | + 'supportsCredentials' => false, |
| 91 | +]); |
| 92 | +``` |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +Released under the MIT License, see [LICENSE](LICENSE). |
| 97 | +The original author of this Library is Alexander <iam.asm89@gmail.com>, while Barry <barryvdh@gmail.com> has been involved since 2015. |
| 98 | +This package is split-off from https://github.com/asm89/stack-cors |
0 commit comments