binjuhor/lumen-cors

跨源资源共享 (CORS)。为 Lumen 微型框架添加 CORS 中间件

v1.0 2019-08-22 15:52 UTC

This package is auto-updated.

Last update: 2024-09-06 09:53:18 UTC


README

Lumen 微型框架的跨源资源共享 (CORS) 中间件。(CORS

安装

按照 Lumen 文档 安装 Lumen 后,从 lumen 文件夹安装 lumen-cors。

使用 Composer 安装

运行 composer require binjuhor/lumen-cors 安装 lumen-cors。

用法

全局 CORS

如果您希望为应用程序的每个 HTTP 请求启用 CORS,请转到您的 bootstrap/app.php 文件,并使用以下行注册您的中间件:

$app->middleware([
    Binjuhor\Lumen\Middleware\CorsMiddleware::class
 ]);

路由 CORS

如果您想为特定路由启用 CORS,您应该在 bootstrap/app.php 文件中为 lumen-cors 中间件分配一个简写键。

$app->routeMiddleware([
    'cors' => 'Binjuhor\Lumen\Middleware\LumenCors',
]);

然后,您在路由选项数组中使用该键。

$app->get('/api/products', ['middleware' => 'cors', function() {
    //Get all products with this route
}]);

更多信息 - Lumen 注册中间件

许可证

MIT