lithemod/cors 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lithemod/cors

Composer 安装命令:

composer require lithemod/cors

包简介

Configures Cross-Origin Resource Sharing (CORS) headers for your Lithe application.

README 文档

README

The CORS (Cross-Origin Resource Sharing) middleware in Lithe configures CORS headers to allow or restrict access to resources from different origins.

Installing the CORS Module

To use the CORS middleware in your Lithe project, you need to install the lithemod/cors package. Follow the steps below:

  1. Make sure you have Composer installed. Composer is a dependency management tool for PHP.

  2. Open your terminal and navigate to your Lithe project directory.

  3. Run the following command to install the module:

    composer require lithemod/cors

    This will download and install the CORS module in your project, as well as automatically register any necessary dependencies.

Using the CORS Middleware

The CORS middleware in Lithe is responsible for configuring Cross-Origin Resource Sharing headers, allowing or restricting access to your application's resources from different origins. To configure it in your application, add it using the use() method:

use Lithe\Middleware\Configuration\cors;

$app->use(cors());

Configuring the CORS Middleware

The CORS middleware can be configured with the following parameters:

  • origins: Allowed origin(s) for accessing your resources. It can be a string or an array of strings. The default is '*' (any origin).
  • methods: Allowed HTTP methods. The default is 'GET, POST, OPTIONS'.
  • headers: Allowed headers. It can be a string or an array of strings. The default is 'Origin, X-Requested-With, Content-Type, Accept'.
  • credentials: Indicates if credentials (such as cookies or authorization headers) are allowed. The default is true.
  • maxAge: Time in seconds for caching the results of a preflight request. The default is null.

Example Configuration:

$app->use(cors([
    'origins' => ['https://example.com', 'https://another.com'],
    'methods' => 'GET, POST, PUT, DELETE, OPTIONS',
    'headers' => ['Content-Type', 'Authorization'],
    'credentials' => true,
    'maxAge' => 86400,
]));

Middleware Behavior

  • Allowed Origins: The middleware sets the Access-Control-Allow-Origin header with the allowed origins.
  • Allowed Methods: Sets the Access-Control-Allow-Methods header with the permitted HTTP methods.
  • Allowed Headers: Sets the Access-Control-Allow-Headers header with the allowed headers.
  • Credentials: Sets the Access-Control-Allow-Credentials header to allow credentials if configured as true.
  • Cache Duration: Sets the Access-Control-Max-Age header to specify the duration for caching preflight requests.
  • Handling OPTIONS Requests: The middleware handles OPTIONS (preflight) requests by returning a 200 OK status and terminating the response.

Considerations

  • Specific Origins: If you specify concrete origins, only those origins will be able to access your resources.
  • HTTP Methods and Headers: Configure the methods and headers according to your application's needs to ensure security and compatibility.
  • Preflight Requests: The middleware ensures that OPTIONS requests are handled properly to support preflight requests.

lithemod/cors 适用场景与选型建议

lithemod/cors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lithemod/cors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lithemod/cors 我们能提供哪些服务?
定制开发 / 二次开发

基于 lithemod/cors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-02