asm89 / stack-cors
Cross-origin resource sharing library and stack middleware
Installs: 153 126 641
Dependents: 24
Suggesters: 13
Security: 0
Stars: 1 275
Watchers: 8
Forks: 56
Open Issues: 2
Requires
- php: ^7.3|^8.0
- symfony/http-foundation: ^5.3|^6|^7
- symfony/http-kernel: ^5.3|^6|^7
Requires (Dev)
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: ^3.5
- dev-master / 2.2.x-dev
- v2.2.0
- v2.1.1
- v2.1.0
- 2.0.x-dev
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- 2.0.0
- 2.0.0-beta3
- 2.0.0-beta2
- 2.0.0-beta1
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.2.1
- 0.2.0
- 0.1.0
- dev-feat-stackonly
- dev-feat-multisymfony
- dev-test-symfony6
- dev-develop
- dev-barryvdh-patch-1
- dev-remove-samehost
- dev-feat-alwaysadd
- dev-feat-maxage
- dev-fix-removecheck
This package is auto-updated.
Last update: 2024-10-29 12:20:54 UTC
README
Library and middleware enabling cross-origin resource sharing for your http-{foundation,kernel} using application. It attempts to implement the W3C Recommendation for cross-origin resource sharing.
Installation
Require asm89/stack-cors
using composer.
Usage
This package can be used as a library or as stack middleware.
Options
The allowedMethods and allowedHeaders options are case-insensitive.
You don't need to provide both allowedOrigins and allowedOriginsPatterns. If one of the strings passed matches, it is considered a valid origin.
If ['*']
is provided to allowedMethods, allowedOrigins or allowedHeaders all methods / origins / headers are allowed.
If supportsCredentials is true
, you must explicitly set allowedHeaders
for any headers which are not CORS safelisted.
Example: using the library
<?php use Asm89\Stack\CorsService; $cors = new CorsService([ 'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'], 'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'], 'allowedOrigins' => ['http://localhost'], 'allowedOriginsPatterns' => ['/localhost:\d/'], 'exposedHeaders' => false, 'maxAge' => 600, 'supportsCredentials' => true, ]); $cors->addActualRequestHeaders(Response $response, $origin); $cors->handlePreflightRequest(Request $request); $cors->isActualRequestAllowed(Request $request); $cors->isCorsRequest(Request $request); $cors->isPreflightRequest(Request $request);
Example: using the stack middleware
<?php use Asm89\Stack\Cors; $app = new Cors($app, [ // you can use ['*'] to allow any headers 'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'], // you can use ['*'] to allow any methods 'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'], // you can use ['*'] to allow requests from any origin 'allowedOrigins' => ['localhost'], // you can enter regexes that are matched to the origin request header 'allowedOriginsPatterns' => ['/localhost:\d/'], 'exposedHeaders' => false, 'maxAge' => 600, 'supportsCredentials' => false, ]);