enlightener/php-cors
Composer 安装命令:
composer require enlightener/php-cors
包简介
Enlightener PHP CORS (Cross-Origin Resource Sharing) is a small library support to prevent attacks from a cross-origin request
README 文档
README
- Configure options for each domain.
- Flexible about the usage way.
- Handle the incoming requests strictly and write exceptions log if any to easy debug.
About
Enlightener PHP CORS is a small library support to prevent attacks from a cross-origin request. I spent a lot of time researching the mechanism, as well as how it works and summarized each line by comments. This library is for the Laravel framework and is also possible for the Symfony framework and the PHP language, but you must modify it if want to use it.
These documents I referenced and listed here:
- MDN Web Docs:
CORS - GitHub:
asm89/stack-cors - GitHub:
fruitcake/laravel-cors
Note
Use for versions of Laravel from 6 to 10 or higher, and the PHP version must be from 7.4
Installation
composer require enlightener/php-cors
Basic Usage
Note
Attributes can be a string separation by comma or an array such as foo, baz | ['foo', 'baz']
Register a cors service
// Default options Cors::origins('*'); // Register one service Cors::origins('https://php.net') ->headers('X-Header-One, X-Header-Two, X-Header-Three') ->methods('GET, HEAD, POST') ->credentials(false) ->exposedHeaders('X-Header-One, X-Header-Two, X-Header-Three') ->maxAge(0); // Register include wildcard in the domain Cors::origin('*.example.com') ->headers('X-Header-One, X-Header-Two, X-Header-Three') ->methods('GET, HEAD, POST'); // Register many services Cors::origins('https://php.net, https://laravel.com') ->headers('X-Header-One, X-Header-Two, X-Header-Three') ->methods('GET, HEAD, POST'); // or Cors::origins(['https://php.net', 'https://laravel.com', '*.example.com']) ->headers(['X-Header-One', 'X-Header-Two', 'X-Header-Three']) ->methods(['GET', 'HEAD', 'POST']) ->exposedHeaders(['X-Header-One', 'X-Header-Two', 'X-Header-Three']); // or Cors::register([ 'origins' => ['https://php.net', 'https://laravel.com', '*.example.com'], 'headers' => ['X-Header-One', 'X-Header-Two', 'X-Header-Three'], 'credentials' => false, 'exposedHeaders' => ['X-Header-One', 'X-Header-Two', 'X-Header-Three'], 'maxAge' => 0 ]); // Retrieve all items in the collection Cors::collection()->items(); // You can use any method that you want to meet the requirements of your project. // Note that to register a cors service always start with the first "origins" method // on each call a CORS facade instance.
Handle a cors service
namespace App\Http\Middleware; use Closure; use Enlightener\Cors\Cors; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; class CorsHandler { /** * Handle an incoming request. */ public function handle(Request $request, Closure $next): Response|JsonResponse|RedirectResponse { Cors::origins(['https://php.net', 'https://laravel.com', 'https://symfony.com']) ->headers(['X-Header-One', 'X-Header-Two', 'X-Header-Three']) ->methods(['GET', 'HEAD', 'POST']); return Cors::handle($request, $next); } }
Configuration
Note
These options are strict, and this means that when you set an option that has the [*] value then it will be equivalent to the work you dynamically handled based on the incoming request. We will not disclose any values unnecessary for the browser side.
| Option | Description | Default value |
|---|---|---|
origins |
Origins are allowed so that the server side can share a resource. | [*] |
methods |
Methods allowed when accessing a resource. | [*] |
headers |
Headers that can be used during the actual request. | [*] |
credentials |
Credentials are allowed such as cookies, tls, client certificates, or authentication headers. |
false |
exposedHeaders |
Headers can be exposed to the browser side. | [] |
maxAge |
The duration in seconds that the results of headers in a preflight request such as access-control-allow: headers, methods can cached. |
0 |
License
The PHP CORS library is licensed under the MIT license.
enlightener/php-cors 适用场景与选型建议
enlightener/php-cors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 464 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「cors」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 enlightener/php-cors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 enlightener/php-cors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 enlightener/php-cors 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Allows multi origin HTTP requests (CORS) supports
A simple proxy service for performing cross-domain AJAX requests.
Alfabank REST API integration
统计信息
- 总下载量: 464
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-20