tenantcloud / laravel-cors
在 Laravel 应用中发送 CORS 头部
v2.4.0
2024-04-25 12:27 UTC
Requires
- php: >=8.2
- illuminate/contracts: ^10.0
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- nunomaduro/larastan: ^2.6
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.8
- php-cs-fixer/shim: ~3.19.2
- phpstan/phpstan: ~1.10.21
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-webmozart-assert: ^1.2
- tenantcloud/php-cs-fixer-rule-sets: ~3.0.0
This package is auto-updated.
Last update: 2024-09-14 20:35:41 UTC
README
Laravel 包用于处理 CORS,支持为每个路由自定义 CORS 配置文件。
安装
在你的 composer.json
文件中添加以下仓库
"repositories": [
{
"type": "git",
"url": "https://github.com/tenantcloud/laravel-cors"
}
],
然后执行 composer require tenantcloud/laravel-cors
安装包。之后,发布配置:php artisan vendor:publish
并选择所需的配置文件。
使用
你希望在整个项目中使用单个全局 CORS
如果你想使用全局配置,将 CorsMiddleware::class
添加到你的 Http\Kernel.php
文件的 $middleware
数组中。将使用配置中的 default
配置。
你希望在路由上使用多个不同的 CORS 配置
如果你需要使用作用域 CORS,你应该
- 将所需的配置添加到 cors.php 文件中
- 将
'cors' => CorsMiddleware::class
添加到你的Http\Kernel.php
文件的$routeMiddleware
数组中。 - 将
cors:your_profile
添加到你想应用 CORS 的路由中。 - 将
OPTIONS
添加到该路由支持的请求方法列表中。
示例
Route::match(['GET', 'OPTIONS'], '/test', 'Controller@test')->middleware('cors:test_profile');