Skip to content

Instantly share code, notes, and snippets.

@Maykonn
Last active August 29, 2015 14:16
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 Maykonn/1c46f84b4f906fd14e64 to your computer and use it in GitHub Desktop.
Save Maykonn/1c46f84b4f906fd14e64 to your computer and use it in GitHub Desktop.
How to enable CORS in Laravel 5.0?

Is common a CORS message when accessing a Laravel Application (or better, an Laravel API), resource via another client application (an Angular.js, Backbone.js, Ember.js, or a PHP, JS, Java, Mobile, app, whatever).

To enable CORS in your Laravel application, create a route group and put inside, all routes that you need to enable CORS:

<?php
Route::group(['after' => 'allowOrigin'], function() {
    // Your fantastic resources
    Route::post('/users', function () {  // ... });
    // ...
});

And finally create an allowOrigin filter that will be applied on Response:

<?php
Route::filter('allowOrigin', function($route, $request, $response) {
    $response->header('Access-Control-Allow-Origin','*');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment