Snippets
Handle Laravel Cors via Middleware
If you want to handle Laravel Cors via middleware, here is the way to do that-
First create a file name Cors.php
public function handle($request, Closure $next)
{
if ($request->isMethod('OPTIONS')){
$response = Response::make();
} else {
$response = $next($request);
}
return $response
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
}
Then go to http\Kernel.php
and added Cors.php
file in the middleware route.
protected $middleware = [
\App\Http\Middleware\Cors::class, //add this line to $middleware variable
]
If you want to allow other headers to your routes, please add them in the 'Access-Control-Allow-Headers' header field.