| 148 |
lars |
1 |
# CORS for PHP (using the Symfony HttpFoundation)
|
|
|
2 |
|
|
|
3 |
[](https://github.com/fruitcake/php-cors/actions)
|
|
|
4 |
[](https://github.com/fruitcake/php-cors/actions)
|
|
|
5 |
[](https://github.com/fruitcake/php-cors/actions/workflows/run-coverage.yml)
|
|
|
6 |
[](http://choosealicense.com/licenses/mit/)
|
|
|
7 |
[](https://packagist.org/packages/fruitcake/php-cors)
|
|
|
8 |
[](https://packagist.org/packages/fruitcake/php-cors)
|
|
|
9 |
[](https://fruitcake.nl/)
|
|
|
10 |
|
|
|
11 |
Library and middleware enabling cross-origin resource sharing for your
|
|
|
12 |
http-{foundation,kernel} using application. It attempts to implement the
|
|
|
13 |
[W3C Recommendation] for cross-origin resource sharing.
|
|
|
14 |
|
|
|
15 |
[W3C Recommendation]: http://www.w3.org/TR/cors/
|
|
|
16 |
|
|
|
17 |
> Note: This is a standalone fork of https://github.com/asm89/stack-cors and is compatible with the options for CorsService.
|
|
|
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. | `[]` |
|
|
|
39 |
| maxAge | Sets the Access-Control-Max-Age response header. | `0` |
|
|
|
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. A wildcard in allowedOrigins will be converted to a pattern.
|
|
|
45 |
|
|
|
46 |
If `['*']` is provided to _allowedMethods_, _allowedOrigins_ or _allowedHeaders_ all methods / origins / headers are allowed.
|
|
|
47 |
|
|
|
48 |
> Note: Allowing a single static origin will improve cacheability.
|
|
|
49 |
|
|
|
50 |
### Example: using the library
|
|
|
51 |
|
|
|
52 |
```php
|
|
|
53 |
<?php
|
|
|
54 |
|
|
|
55 |
use Fruitcake\Cors\CorsService;
|
|
|
56 |
|
|
|
57 |
$cors = new CorsService([
|
|
|
58 |
'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
|
|
|
59 |
'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
|
|
|
60 |
'allowedOrigins' => ['http://localhost', 'https://*.example.com'],
|
|
|
61 |
'allowedOriginsPatterns' => ['/localhost:\d/'],
|
|
|
62 |
'exposedHeaders' => ['Content-Encoding'],
|
|
|
63 |
'maxAge' => false,
|
|
|
64 |
'supportsCredentials' => false,
|
|
|
65 |
]);
|
|
|
66 |
|
|
|
67 |
$cors->addActualRequestHeaders(Response $response, $origin);
|
|
|
68 |
$cors->handlePreflightRequest(Request $request);
|
|
|
69 |
$cors->isActualRequestAllowed(Request $request);
|
|
|
70 |
$cors->isCorsRequest(Request $request);
|
|
|
71 |
$cors->isPreflightRequest(Request $request);
|
|
|
72 |
```
|
|
|
73 |
|
|
|
74 |
## License
|
|
|
75 |
|
|
|
76 |
Released under the MIT License, see [LICENSE](LICENSE).
|
|
|
77 |
|
|
|
78 |
> This package is split-off from https://github.com/asm89/stack-cors and developed as stand-alone library since 2022
|