vendredi 29 mars 2019

CORS Laravel 5.7 does not work correct, is itcore bug?

I use Laravel 5.7 and middleware is:

<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

      header('Access-Control-Allow-Origin: *');


        $headers = [
            'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
        ];
        if($request->getMethod() == "OPTIONS") {
            return Response::make('OK', 200, $headers);
        }


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

    }
}

I have registered this in Kernel.php:

protected $routeMiddleware = [
    'cors' => \App\Http\Middleware\Cors::class
];

So, when browser sends OPTION request server returns the following instead CORS settings:

enter image description here

Route is:

Route::group(['middleware' => ['cors']], function () {
});

It means cors middleware does not work correct.

I tried simple test:

public function handle($request, Closure $next)
{

   echo 'test';

}

Server does not return me echo 'test'; when request is sending



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire