mangel1196 / php-cors
Symfony HttpFoundation 的跨域资源共享库
dev-main
2023-07-19 22:24 UTC
Requires
- php: ^7.4|^8.0|^8.1
- symfony/http-foundation: ^4.4|^5.4|^6
Requires (Dev)
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: ^3.5
This package is not auto-updated.
Last update: 2024-09-27 02:54:34 UTC
README
通过应用程序启用 http-{foundation,kernel} 的跨域资源共享的库和中间件。它试图实现 W3C 建议 的跨域资源共享。
注意:这是 https://github.com/asm89/stack-cors 的独立分支,并且与 CorsService 的选项兼容。
安装
使用 composer 需要 fruitcake/php-cors
。
使用方法
此软件包可以用作库。您可以使用它通过以下方式在框架中使用
选项
allowedMethods 和 allowedHeaders 选项不区分大小写。
您不需要同时提供 allowedOrigins 和 allowedOriginsPatterns。如果传入的字符串之一匹配,则认为它是一个有效的源。allowedOrigins 中的通配符将被转换为模式。
如果向 allowedMethods、allowedOrigins 或 allowedHeaders 提供了 ['*']
,则允许所有方法/源/头。
注意:允许单个静态源将提高缓存性。
示例:使用库
<?php use Fruitcake\Cors\CorsService; $cors = new CorsService([ 'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'], 'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'], 'allowedOrigins' => ['https://', 'https://*.example.com'], 'allowedOriginsPatterns' => ['/localhost:\d/'], 'exposedHeaders' => ['Content-Encoding'], 'maxAge' => 0, 'supportsCredentials' => false, ]); $cors->addActualRequestHeaders(Response $response, $origin); $cors->handlePreflightRequest(Request $request); $cors->isActualRequestAllowed(Request $request); $cors->isCorsRequest(Request $request); $cors->isPreflightRequest(Request $request);
许可证
根据 MIT 许可证发布,请参阅 LICENSE。
此软件包自 2022 年从 https://github.com/asm89/stack-cors 中分离出来,并作为独立库开发。