Skip to content

Instantly share code, notes, and snippets.

@earth774
Created March 9, 2019 13:23
Show Gist options
  • Save earth774/aa73e0d9be5aacb5a323ccd3a8b4e6e1 to your computer and use it in GitHub Desktop.
Save earth774/aa73e0d9be5aacb5a323ccd3a8b4e6e1 to your computer and use it in GitHub Desktop.
Create file CorsMiddleware.php allow origin
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Http\Middleware;
use Closure;
/**
* Description of CorsMiddleware
*
* @author Amiearth
*/
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment