中间件实现跨源资源共享(CORS)

v2.0.2 2022-10-27 15:22 UTC

This package is auto-updated.

Last update: 2024-08-27 19:31:54 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

使用 neomerx/cors-psr7 实现跨源资源共享(CORS)的中间件。

要求

安装

此包可以通过Composer安装和自动加载,地址为middlewares/cors

composer require middlewares/cors

示例

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());

使用方法

您必须向构造函数提供一个 Neomerx\Cors\Contracts\AnalyzerInterface。有关更多信息,请参阅neomerx/cors-psr7。可选地,您还可以将一个 Psr\Http\Message\ResponseFactoryInterface 作为第二个参数提供给构造函数以创建响应。如果没有定义,将使用 Middleware\Utils\Factory 自动检测。

$analyzer = Analyzer::instance($settings);
$responseFactory = new MyOwnResponseFactory();

$cors = new Middlewares\Cors($analyzer, $responseFactory);

有关最近更改的更多信息,请参阅CHANGELOG,有关贡献详情的更多信息,请参阅CONTRIBUTING

MIT许可(MIT)。有关更多信息,请参阅LICENSE