0

I'm trying to enable CORS in Lumen 5.8 backend api system, called by a React Js frontend.

Here what I did:

  1. I've created CatchAllOptionsRequestsProvider.php to App\Providers folder.
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. Then I've created 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. In bootstrap/app.php I've added this:
$app->middleware([
   App\Http\Middleware\CorsMiddleware::class
]);

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

As result I get this error in bootstrap/app.php file: PHP Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Application::middleware() in /bootstrap/app.php:44

I don't wanna install any package, I've already tried without getting results, I'd like just to fix this error, because I guess it could be work. Could you please help me??

Thanks in advance for your answers.

1 Answers1

0

Your title says Lumen, and bootstrap/app.php is indicative of Lumen, but your error says that your $app variable is an instance of Illuminate\Foundation\Application, which does not make sense.

Illuminate\Foundation\Application is the IOC container used in Laravel, not Lumen. And, as your error indicates, it does not have a middleware() method.

Your $app variable should be an instance of Laravel\Lumen\Application, which is Lumen's IOC container. This container does have the middleware() method you're looking for.


I know you argued against it, but I would suggest using a package to implement CORS.

  • fruitcake/laravel-cors - This package is the one that is included in Laravel by default as of 7.0. It also supports Lumen.
  • spatie/laravel-cors - This is another popular package that supports Lumen. It was archived when Laravel included CORS by default, but is still available for use for older versions.

Both packages have detailed instructions on installation and configuration, and will handle all the CORS details for you.

patricus
  • 59,488
  • 15
  • 143
  • 145
  • I'm using Lumen 5.8.35 and I cannot upgrade. fruitcake doesn't produce any effect. Spatie/laravel-cors is abandoned. Can you help me understand how $app could be an instance of Laravel\Lumen\Application with an example? – Andrea Bianchi Oct 27 '21 at 18:55
  • @AndreaBianchi I think you would need to post the rest of your bootstrap/app.php file (with comments removed). At the top should be a line like `$app = new Laravel\Lumen\Application(dirname(__DIR__));`. If you changed this line to instead use `Illuminate\Foundation\Application`, that's not going to work. – patricus Oct 28 '21 at 14:06