Laravel文档阅读笔记-How to enable CORS on Laravel

使用中间件为http响应头添加Access-Control-Allow-Origin

1.创建Cors中间件:

$ php artisan make:middleware Cors

2.编辑中间件:

header('Access-Control-Allow-Origin', '*')

    }
}

3.将中间件添加到Kernel.php中:

protected $routeMiddleware = [
        'auth'          => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic'    => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings'      => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can'           => \Illuminate\Auth\Middleware\Authorize::class,
        'guest'         => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed'        => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle'      => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'cors'          => \App\Http\Middleware\Cors::class, // added
    ];

4.设置中间件对应的路由:

Route::middleware(['cors'])->group(function () {
    Route::post('/hogehoge', 'Controller@hogehoge');
});

Laravel文档阅读笔记-How to enable CORS on Laravel_第1张图片

 

你可能感兴趣的:(php,laravel,笔记,php)