Middleware to implement Cross-Origin Resource Sharing (CORS)

Package info

github.com/middlewares/cors

pkg:composer/middlewares/cors

Statistics

Installs: 74 941

Dependents: 2

Suggesters: 0

Stars: 13

Open Issues: 0

v2.1.0 2025-03-23 10:17 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 5c18b616fccde2e8e0a8107dfafee0537e87c8e0

httpsecuritycorsmiddlewarepsr-7cross-originpsr-15

This package is auto-updated.

Last update: 2026-06-26 17:42:05 UTC


README

👁 Latest Version on Packagist
👁 Software License
👁 Testing
👁 Total Downloads

Middleware to implement Cross-Origin Resource Sharing (CORS) using neomerx/cors-psr7.

Requirements

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.