Created
April 27, 2017 10:51
-
-
Save casoetan/697bc4c36fc92afd547d1feb455669e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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