CORS adalah fitur security untuk mencegah akses yang tidak sah. Ini adalah singkatan dari Cross-Origin Resource Sharing.
Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. ~wikipedia
Configuration
Create Cors middleware
php artisan make:middleware Cors
Config CORS Middleware
Konfigurasi pengaturan cors di app/Hsttp/Middleware/CorsMiddleware.php
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
Registering CORS Middleware
Aktifkan CORS untuk semua route di file app/Http/Kernel.php
'cors' => \App\Http\Middleware\Cors::class,
Implementation CORS Middleware
Implementasikan CORS Middleware di route/api.php
Route::group(['middleware' => 'cors'], function () {
Route::get('/v1/employees/{id?}', 'Employees@index');
Route::post('/v1/employees', 'Employees@store');
Route::post('/v1/employees/{id}', 'Employees@update');
Route::delete('/v1/employees/{id}', 'Employees@destroy');
});