Created
March 9, 2019 13:23
-
-
Save earth774/aa73e0d9be5aacb5a323ccd3a8b4e6e1 to your computer and use it in GitHub Desktop.
Create file CorsMiddleware.php allow origin
This file contains hidden or 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 | |
/* | |
* 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