Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shaunlee
Last active March 14, 2017 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunlee/a9186048d5635a986cacb481fcaed120 to your computer and use it in GitHub Desktop.
Save shaunlee/a9186048d5635a986cacb481fcaed120 to your computer and use it in GitHub Desktop.
Lumen with CORS and OPTIONS requests
<?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', '*');
$response->header('Access-Control-Allow-Credentials', 'true');
$response->header('Access-Control-Max-Age', 86400);
// Sends it
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment