Ahmad Abo Zaid
Ahmad Abo Zaid

Reputation: 11

Laravel API CORS

I created API using Laravel 5.6 and i created CORS middleware and this is the content of middleware , but when access this api from another Ip address it's return the data ..

Route::group([

'middleware' => 'cors',

], function ($router) {


Route::get('/get_users', function(){
     return App\User::all();
});

});

I registered the cors middleware in Kernal.php

Upvotes: 1

Views: 2897

Answers (2)

Anana Aristotle
Anana Aristotle

Reputation: 344

Just add this to the beginning of your api route file:

header('Access-Control-Allow-Origin : *');
header('Access-Control-Allow-Headers : Content-Type,X-Auth-Token,Authorization,Origin');
header('Access-Control-Allow-Methods :GET, POST, PUT, DELETE, OPTIONS');

This work for me.

Upvotes: 1

Mahbub
Mahbub

Reputation: 4942

You may use barryvdh/laravel-cors package instead of writing your own middleware.

Upvotes: 1

Related Questions