drewlabs/cors
Composer 安装命令:
composer require drewlabs/cors
包简介
HTTP Cors request handlers
README 文档
README
This library provides HTTP cors handlers for PHP projects based on Psr7 requests.
Usage
Default instance
The library comes with a default instance that can be created through it constructor method:
use Drewlabs\Cors\Cors; $cors = new Cors();
Note The example above uses the default constructor which creates the instance using default configuration values which does not allow any header, nor host. To customize the default configuration, the constructor
accepts a configuration dictionnary (PHP array) as parameter.
- Accepting a specific list of origin or hosts
The example below configures the cors instance to accept request only from http://localhost:3000 as origin.
use Drewlabs\Cors\Cors; $cors = new Cors([ 'allowed_hosts' => 'http://localhost:3000', 'allowed_headers' => ['*'], 'allowed_methods' => ['*'] ]);
Note The example above configure the cors to allow request from http://localhost:3000, using any headers and any methods.
Using configuration builder
To avoid typo errors, the library provide a fluent builder instance for building configuration values:
use Drewlabs\Cors\Cors; use Drewlabs\Cors\ConfigurationBuilder; $cors = new Cors( ConfigurationBuilder::new() // Add `Origin: http://localhost` header ->withHosts('http://localhost', 'http://localhost:3000') // Add an `allowed_credentials: yes` header ->withCredentials() ->withMaxAge(0) // Add an `Access-Control-Allow-Methods: POST` header configuration value ->withMethods('POST') // Convert the builded configuration to array ->toArray() );
Handling Psr request
The library provides a handleRequest method for handling PSR7 compatible requests.
use Drewlabs\Cors\Cors; $cors = new Cors(); $cors->handleRequest(new Request());
Checking if request is a cors request
Sometimes you might want to make sure if the request has Origin header, and is a cors request (a.k.a request method has value equals OPTION):
use Drewlabs\Cors\Cors; use Drewlabs\Cors\ConfigurationBuilder; $cors = new Cors( ConfigurationBuilder::new() // Add `Origin: http://localhost` header ->withHosts('*') // Convert the builded configuration to array ->toArray() ); // Checks if the Psr request is a cors request $cors->isCorsRequest(new Request());
drewlabs/cors 适用场景与选型建议
drewlabs/cors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 436 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 drewlabs/cors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drewlabs/cors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 436
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-21