Laravel CORS Middleware

What is CORS?

Let’s get started

php artisan make:middleware corsMiddleware
<?phpnamespace App\Http\Middleware;use Closure;class corsMiddleware{/*** @param $request* @param Closure $next* @return mixed*/public function handle($request, Closure $next) {$response = $next($request);$response->headers->set('Access-Control-Allow-Origin' , '*');$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application','ip');return $response;}}
‘cors’ => \App\Http\Middleware\CORS::class,
Route::group([‘prefix’ => ‘auth’, ‘middleware’ => ‘cors’], function() {Route::post(‘/login’, ‘AuthController@login’);Route::post(‘/register’, ‘AuthController@register’);});



Back-end developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store