Step 1. Open up command prompt or your terminal. Modification your existing working directory to your Laravel task directory.
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!