compwright / psr-cors
PHP interop cross-origin resource sharing (CORS) library and middleware
Requires
- php: ^8.2
- psr/http-factory: ^1.1
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.65
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-12-24 01:59:44 UTC
README
Library and middleware enabling cross-origin resource sharing (CORS) for your PHP interoperable application, utilizing the PSR-7 and PSR-15 standards.
It attempts to implement the W3C Recommendation for cross-origin resource sharing.
Installation
Require compwright/psr-cors
using composer.
Usage
This package can be used as a library or as PSR-15 middleware.
Options
The allowedMethods and allowedHeaders options are case-insensitive.
If true
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 middleware
<?php use Compwright\PsrCors\Middleware; $middleware = Middleware::create( responseFactory: $psrResponseFactory, allowedHeaders: ['x-allowed-header', 'x-other-allowed-header'], allowedMethods: ['DELETE', 'GET', 'POST', 'PUT'], allowedOrigins: ['localhost'], exposedHeaders: [], maxAge: 600, supportsCredentials: false ); $response = $middleware->handle($request, $app);