laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

Cors Issue #1040

Closed noxsii closed 4 years ago

noxsii commented 4 years ago

Description:

CORS Policy: Have the problem that I permanently have the error with Cors. I've made several attempts to fix this. But unfortunately without success. I try to add the Middleware global with:

$app->routeMiddleware([ App\Http\Middleware\Cors::class, ..... ]);

The Middleware:

`namespace App\Http\Middleware; use Closure;

class Cors { protected $settings = array( 'origin' => '*', // Wide Open! 'allowMethods' => 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', );

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;
}

}`

I add the Header in the the routes/web,php with:

header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE'); header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Origin, Authorization');

unfortunately none of the solutions led to success. Someone a solution or a suggestion?

Work with vue.js local :)

driesvints commented 4 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.