asm89 / stack-cors
跨域资源共享库和中间件
v2.2.0
2023-11-14 13:51 UTC
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-08-29 11:56:17 UTC
README
为您的 http-{基础,内核} 应用程序启用跨域资源共享的库和中间件。它试图实现W3C 建议的跨域资源共享。
安装
使用 composer 安装 asm89/stack-cors
。
用法
此包可以作为库或作为stack 中间件使用。
选项
allowedMethods 和 allowedHeaders 选项不区分大小写。
您无需同时提供 allowedOrigins 和 allowedOriginsPatterns。如果传递的字符串之一匹配,则被认为是有效的源。
如果将 ['*']
提供给 allowedMethods、allowedOrigins 或 allowedHeaders,则允许所有方法/源/头部。
如果 supportsCredentials 为 true
,则必须为任何不属于 CORS 安全列表的头部显式设置 allowedHeaders
。
示例:使用库
<?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);
示例:使用中间件
<?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, ]);