Skip to content

Instantly share code, notes, and snippets.

@casoetan
Created April 27, 2017 10:51
Show Gist options
  • Save casoetan/697bc4c36fc92afd547d1feb455669e5 to your computer and use it in GitHub Desktop.
Save casoetan/697bc4c36fc92afd547d1feb455669e5 to your computer and use it in GitHub Desktop.
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment