HTML 5 has actually introduced brand-new standard Cross-origin resource sharing (CORS) which permits web applications to define which origins (website or domains) are allowed to gain access to resources on the server.
Dealing With Cross-Origin Request (CORS) Blocked, Laravel access-control-allow-origin error in Laravel 5.5
When you are sending out a request from Ajax, Angular js, React js, Vue js or other javascript or front-end framework from one domain or website to other domain, site or server, you might face listed below errors like following
Cross-Origin Demand Blocked: The Same Origin Plan disallows checking out the remote resource at. (Reason: CORS preflight channel did not prosper).
In this tutorial I am going to show you how to deal with Cross-Origin Request Blocked, CORS preflight network did not succeed, CORS preflight Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers errors in Laravel 5, Laravel 5.3, Laravel 5.4 or above.
Actions to fix or solve Cross-Origin Request Blocked error in Laravel 5.5.
Step 1. Open up command prompt or your terminal. Modification your existing working directory to your Laravel task directory.
Instance:
cd var/www/html/laravel
Step 2. install barryvdh/laravel-cors package. By running listed below command. It will take couple of minutes.composer call for barryvdh/laravel-cors.
Step 3. Once the installation procedure is finished run listed below command to release the vendor files.
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
Above command will develop a file named cors.php in your config directory. Open up that file make the changes according to your demand.
<?php
return [
/*
|--------------------------------------------------------------------------
| Laravel CORS
|--------------------------------------------------------------------------
|
| allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
| to accept any value.
|
*/
'supportsCredentials' => false,
'allowedOrigins' => ['*'],// ex: ['abc.com', 'api.abc.com']
'allowedHeaders' => ['*'],
'allowedMethods' => ['*'],// ex: ['GET', 'POST', 'PUT', 'DELETE']
'exposedHeaders' => [],
'maxAge' => 0,
];
Step 4. Register the cors middleware in the application.Open app/Http/kernal. php as well as change listed below code in your $routeMiddleware array
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'cors' => \Barryvdh\Cors\HandleCors::class, // add this line to enable cors to your routes
];
For global usage modify below code
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\Barryvdh\Cors\HandleCors::class,
];
Step 5. Following using cor middleware in routes to enable cors.
Note: Make certain you return a json as outcome;
Route::post('/api/your_url', ['middleware' => 'cors',function(){
return ['status'=>'success'];
}]);
OR
Route::post('/api/your_url', function () {
return ['status'=>'success'];
})->middleware('cors');
OR
Route::group(['middleware' => 'cors'], function() {
Route::post('/api/your_url', function () {
return ['status'=>'success'];
});
});
Route::group(['middleware' => 'cors'], function() {
Route::post('/api/your_url','YourController@function' );
});
Step 6. Open app/Http/Middleware/ VerifyCsrfToken.php as well as allow api/ url groups to by pass VerifyCsrfToken middleware.
protected $except = [
'/webpush','/webpush/*','api/*'
];
You might have effectively configured and also Cors on Laravel 5.5. If still you face any kind of issue, please remark listed below or you can ask our Laravel experts we are happy to assist you!