Skip to content

Instantly share code, notes, and snippets.

@Scarwolf
Created May 31, 2018 12:06
Show Gist options
  • Save Scarwolf/89a4fa9ddd0683292d64812cb14e4a64 to your computer and use it in GitHub Desktop.
Save Scarwolf/89a4fa9ddd0683292d64812cb14e4a64 to your computer and use it in GitHub Desktop.
Laravel 5.6 CORS Middleware
// Full File Path: app/Http/Middleware/CORS.php
<?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) {
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin' , '*');
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
return $response;
}
}
// Full File Path: app/Http/Kernel.php
protected $middleware = [
[...]
\App\Http\Middleware\CORS::class
];
protected $routeMiddleware = [
[...]
'cors' => \App\Http\Middleware\CORS::class
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment