middlewares / cors
Middleware to implement Cross-Origin Resource Sharing (CORS)
Installs: 61 663
Dependents: 2
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^3.0
- neomerx/cors-psr7: ^2.0 || ^3.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to implement Cross-Origin Resource Sharing (CORS) using neomerx/cors-psr7.
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/cors.
composer require middlewares/cors
Example
use Neomerx\Cors\Strategies\Settings; use Neomerx\Cors\Analyzer; $settings = new Settings(); $settings->setServerOrigin('http', 'example.com', 123); $analyzer = Analyzer::instance($settings); $dispatcher = new Dispatcher([ new Middlewares\Cors($analyzer) ]); $response = $dispatcher->dispatch(new ServerRequest());
Usage
You have to provide a Neomerx\Cors\Contracts\AnalyzerInterface
to the constructor. See neomerx/cors-psr7 for more info. Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface
as the second argument to create the responses. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.
$analyzer = Analyzer::instance($settings); $responseFactory = new MyOwnResponseFactory(); $cors = new Middlewares\Cors($analyzer, $responseFactory);
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.