effectra/cors
Composer 安装命令:
composer require effectra/cors
包简介
PHP library for enabling CORS (Cross-Origin Resource Sharing) in HTTP requests/responses. It follows PSR guidelines, and can be easily integrated into PHP projects.
README 文档
README
Effectra/cors is a PHP package that provides middleware and service classes for handling Cross-Origin Resource Sharing (CORS) in web applications. CORS is a security feature implemented by web browsers to control access to resources on a different origin.
Installation
You can install the Effectra/cors package via Composer:
composer require effectra/cors
Usage
CorsMiddleware
The CorsMiddleware class is a PSR-15 middleware that can be used in a middleware stack to handle CORS requests. It checks incoming requests for CORS-related headers and adds appropriate headers to the response.
Example:
use Effectra\Cors\CorsMiddleware; use Effectra\Cors\CorsService; use Psr\Http\Server\MiddlewareInterface; // Create a CorsService instance with desired configuration $corsService = new CorsService([ 'allowedOrigins' => ['https://example.com'], 'allowedMethods' => ['GET', 'POST'], 'allowedHeaders' => ['Content-Type'], ]); // Create the CorsMiddleware instance $corsMiddleware = new CorsMiddleware($corsService, ['/api']); // Add the middleware to your middleware stack $middlewareStack = [ // Other middleware... $corsMiddleware, // Other middleware... ];
CorsService
The CorsService class provides the core functionality for handling CORS requests. It allows you to configure various CORS-related settings.
Example:
use Effectra\Cors\CorsService; // Create a CorsService instance with desired configuration $corsService = new CorsService([ 'allowedOrigins' => ['https://example.com'], 'allowedMethods' => ['GET', 'POST'], 'allowedHeaders' => ['Content-Type'], ]); // Check if a request is a CORS request $isCorsRequest = $corsService->isCorsRequest($request); // Check if a request is a preflight request $isPreflightRequest = $corsService->isPreflightRequest($request); // Handle a preflight request and add necessary headers to the response $preflightResponse = $corsService->handlePreflightRequest($request); // Check if a specific origin is allowed $isOriginAllowed = $corsService->isOriginAllowed($request); // Add CORS headers to the actual response $actualResponse = $corsService->addActualRequestHeaders($response, $request);
Configuration
The CorsService class allows you to configure various CORS-related settings using an options array.
Available Options:
allowedOrigins: An array of allowed origins.allowedOriginsPatterns: An array of allowed origin patterns (wildcards).allowedMethods: An array of allowed HTTP methods.allowedHeaders: An array of allowed headers.supportsCredentials: Whether credentials (cookies, HTTP authentication) should be supported.maxAge: Maximum time (in seconds) that the results of a preflight request can be cached.exposedHeaders: An array of headers exposed to the response.
Contributing
If you'd like to contribute to this project, please follow our contribution guidelines.
License
The Effectra/cors package is open-sourced software licensed under the MIT license.
effectra/cors 适用场景与选型建议
effectra/cors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 effectra/cors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 effectra/cors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-05