dimanche 1 novembre 2015

Laravel 5 middleware not working for group of routes

I am appling cors middleware to access json from some other domain.

This code works just fine :-

Route::get('api/authenticate', ['middleware' => 'cors', function() {
    return Response::json(array('name' => 'Steve Jobs', 'state' => 'California'));
}]);

But when I apply it to :-

Route::group(['prefix' => 'api', 'middleware' => 'cors'], function()
{
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});

it gives me error in console,

No 'Access-Control-Allow-Origin' header is present on the requested resource

I have a have added Cors middleware, cors.php :-

class Cors
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    return $next($request)->header('Access-Control-Allow-Origin', '*')->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
}

Please help. Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire