Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Created March 30, 2017 23:10
Show Gist options
  • Save paulofreitas/939acc7d2b3634329124ab742f814e37 to your computer and use it in GitHub Desktop.
Save paulofreitas/939acc7d2b3634329124ab742f814e37 to your computer and use it in GitHub Desktop.
CORS middleware example
<?php
namespace App\Http\Middleware;
class HandleCorsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request)
->header('Access-Control-Allow-Origin', 'http://example.com/') // * = any origin
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') // * = any method
->header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With') // * = any header
->header('Access-Control-Allow-Credentials', 'true');
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment