Skip to content

Instantly share code, notes, and snippets.

@danharper
Last active November 4, 2025 12:42
Show Gist options
  • Select an option

  • Save danharper/06d2386f0b826b669552 to your computer and use it in GitHub Desktop.

Select an option

Save danharper/06d2386f0b826b669552 to your computer and use it in GitHub Desktop.

Revisions

  1. danharper revised this gist May 1, 2015. 2 changed files with 16 additions and 16 deletions.
    16 changes: 8 additions & 8 deletions CatchAllOptionsRequestsProvider.php
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,14 @@
    */
    class CatchAllOptionsRequestsProvider extends ServiceProvider {

    public function register()
    {
    $request = app('request');
    public function register()
    {
    $request = app('request');

    if ($request->isMethod('OPTIONS'))
    {
    app()->options($request->path(), function() { return response('', 200); });
    }
    }
    if ($request->isMethod('OPTIONS'))
    {
    app()->options($request->path(), function() { return response('', 200); });
    }
    }

    }
    16 changes: 8 additions & 8 deletions CorsMiddleware.php
    Original file line number Diff line number Diff line change
    @@ -2,15 +2,15 @@

    class CorsMiddleware {

    public function handle($request, \Closure $next)
    {
    $response = $next($request);
    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', '*');
    $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;
    }
    return $response;
    }

    }
  2. danharper created this gist May 1, 2015.
    21 changes: 21 additions & 0 deletions CatchAllOptionsRequestsProvider.php
    Original 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); });
    }
    }

    }
    16 changes: 16 additions & 0 deletions CorsMiddleware.php
    Original 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;
    }

    }
    7 changes: 7 additions & 0 deletions usage.md
    Original 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