Last active
November 4, 2025 12:42
-
Star
(181)
You must be signed in to star a gist -
Fork
(41)
You must be signed in to fork a gist
-
-
Save danharper/06d2386f0b826b669552 to your computer and use it in GitHub Desktop.
Revisions
-
danharper revised this gist
May 1, 2015 . 2 changed files with 16 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,14 +8,14 @@ */ class CatchAllOptionsRequestsProvider extends ServiceProvider { public function register() { $request = app('request'); if ($request->isMethod('OPTIONS')) { app()->options($request->path(), function() { return response('', 200); }); } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -2,15 +2,15 @@ class CorsMiddleware { public function handle($request, \Closure $next) { $response = $next($request); $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', '*'); return $response; } } -
danharper created this gist
May 1, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; /** * If the incoming request is an OPTIONS request * we will register a handler for the requested route */ class CatchAllOptionsRequestsProvider extends ServiceProvider { public function register() { $request = app('request'); if ($request->isMethod('OPTIONS')) { app()->options($request->path(), function() { return response('', 200); }); } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ <?php namespace App\Http\Middleware; class CorsMiddleware { public function handle($request, \Closure $next) { $response = $next($request); $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', '*'); return $response; } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ Register the `CatchAllOptionsRequestsProvider` service provider in `bootstrap/app.php` which will check the incoming request and response successfully if it is an `OPTIONS` request. Add the `CorsMiddleware` to the `$app->middleware([` array in `bootstrap/app.php` which will attach the following CORS headers to all responses: * allow all headers * allow requests from all origins * allow all the headers which were provided in the request