【问题标题】:Lumen 5.8 enable corsLumen 5.8 启用 cors
【发布时间】:2021-12-12 23:51:41
【问题描述】:

我正在尝试在 Lumen 5.8 后端 api 系统中启用 CORS,由 React Js 前端调用。

我做了什么:

  1. 我已在 App\Providers 文件夹中创建了 CatchAllOptionsRequestsProvider.php。
use Illuminate\Support\ServiceProvider;
/**
 * If the incoming request is an OPTIONS request
 * we will register a handler for the requested route
 */
class CatchAllOptionsRequestsProvider extends ServiceProvider {
  public function register()
  {
    $request = app('request');
    if ($request->isMethod('OPTIONS'))
    {
      app()->options($request->path(), function() { return response('', 200); });
    }
  }
}
  1. 然后我创建了 CorsMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;

class CorsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //Intercepts OPTIONS requests
        if($request->isMethod('OPTIONS')) {
            $response = response('', 200);
        } else {
            // Pass the request to the next middleware
            $response = $next($request);
        }

        // Adds headers to the response
        $response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE');
        $response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
        $response->header('Access-Control-Allow-Origin', '*');

        // Sends it
        return $response;
    }
}
  1. 在 bootstrap/app.php 我添加了这个:
$app->middleware([
   App\Http\Middleware\CorsMiddleware::class
]);

$app->register(App\Providers\CatchAllOptionsRequestsProvider::class);

结果我在 bootstrap/app.php 文件中得到这个错误: PHP 致命错误:未捕获错误:调用 /bootstrap/app.php:44 中未定义的方法 Illuminate\Foundation\Application::middleware()

我不想安装任何软件包,我已经尝试过但没有得到结果,我只想修复这个错误,因为我想它可能会工作。你能帮帮我吗?

提前感谢您的回答。

【问题讨论】:

    标签: php laravel cors lumen


    【解决方案1】:

    你的标题说 Lumen,bootstrap/app.php 表示 Lumen,但你的错误说你的 $app 变量是 Illuminate\Foundation\Application 的一个实例,这没有意义。

    Illuminate\Foundation\Application 是 Laravel 中使用的 IOC 容器,而不是 Lumen。而且,正如您的错误所示,它没有 middleware() 方法。

    您的$app 变量应该是Laravel\Lumen\Application 的一个实例,它是Lumen 的IOC 容器。此容器确实具有您正在寻找的 middleware() 方法。


    我知道你反对它,但我建议使用包来实现 CORS。

    • fruitcake/laravel-cors - 这个包是 Laravel 7.0 中默认包含的包。它还支持 Lumen。
    • spatie/laravel-cors - 这是另一个支持 Lumen 的流行包。它在 Laravel 默认包含 CORS 时已存档,但仍可用于旧版本。

    这两个软件包都有详细的安装和配置说明,并且会为您处理所有 CORS 详细信息。

    【讨论】:

    • 我使用的是 Lumen 5.8.35,无法升级。水果蛋糕不会产生任何效果。 Spatie/laravel-cors 被废弃。你能通过一个例子帮助我理解 $app 如何成为 Laravel\Lumen\Application 的一个实例吗?
    • @AndreaBianchi 我认为您需要发布其余的 bootstrap/app.php 文件(删除了 cmets)。顶部应该是像$app = new Laravel\Lumen\Application(dirname(__DIR__)); 这样的行。如果您将此行更改为使用Illuminate\Foundation\Application,那将无法正常工作。
    猜你喜欢
    • 2019-12-27
    • 2020-03-02
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2020-07-21
    • 2019-01-14
    • 2018-12-09
    相关资源
    最近更新 更多