tenantcloud/laravel-cors

在 Laravel 应用中发送 CORS 头部

v2.4.0 2024-04-25 12:27 UTC

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,你应该

  1. 将所需的配置添加到 cors.php 文件中
  2. 'cors' => CorsMiddleware::class 添加到你的 Http\Kernel.php 文件的 $routeMiddleware 数组中。
  3. cors:your_profile 添加到你想应用 CORS 的路由中。
  4. OPTIONS 添加到该路由支持的请求方法列表中。

示例

Route::match(['GET', 'OPTIONS'], '/test', 'Controller@test')->middleware('cors:test_profile');