stargatesg1's avatar

Cors in Lumen 5.8

I followed this tutorial but I an still getting CORS issues. Here is a link to the tutorial

https://www.codementor.io/chiemelachinedum/steps-to-enable-cors-on-a-lumen-api-backend-e5a0s1ecx

I am calling the following route via AJAX

Here is the Route

$router->post('/api/customer', 'customer@update');
0 likes
5 replies
bobbybouwmann's avatar

Did you register the middleware correctly? Did you change anything from the code they provided?

Just need some more information to help you out here

stargatesg1's avatar

This is what I have so far

app\Http\Middleware\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)
    {
        $headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With'
        ];

        if ($request->isMethod('OPTIONS'))
        {
            return response()->json('{"method":"OPTIONS"}', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
        {
            $response->header($key, $value);
        }

        return $response;
    }
}

In this file I have this

bootstrap/app.php

$app->middleware([
    App\Http\Middleware\CorsMiddleware::class
 ]);

I am not sure what I am missing

akahen's avatar

I'm having the same issue. I have the CorsMiddleware set up the same and it has been working fine. I added a new route to post a form to and getting the Cors error again. Working on my dev machine yet failing on our live site hosting VueJs app on AWS S3.

hemmy6894's avatar

@akahen make sure the request goes to the end if you break the request with die() or dd() you will get CORS error

this work fine in post man not in js app

Please or to participate in this conversation.