如何在 Laravel 中启用 CORS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55346154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable CORS in Laravel?
提问by cyber8200
I am in Laravel 5.8 - I kept getting this CORS issue
我在 Laravel 5.8 - 我一直收到这个 CORS 问题
I've tried
我试过了
php artisan make:middleware Cors
Add these code
添加这些代码
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->header(‘Access-Control-Allow-Origin', ‘*')
->header(‘Access-Control-Allow-Methods', ‘GET, POST, PUT, DELETE, OPTIONS')
->header(‘Access-Control-Allow-Headers', ‘X-Requested-With, Content-Type, X-Token-Auth, Authorization');
}
}
restart my local Apache 2 sudo apachectl -k restart
重新启动我的本地 Apache 2 sudo apachectl -k restart
Open up app/Http/Kernel.php
- added this 1 line
打开app/Http/Kernel.php
- 添加了这 1 行
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,
'admin' => \App\Http\Middleware\AdminMiddleware::class,
'dev' => \App\Http\Middleware\DevMiddleware::class,
'cors' => \App\Http\Middleware\Cors::class, <-----
];
refresh the site, go to console, still see the same CORS issue
刷新站点,转到控制台,仍然看到相同的 CORS 问题
How would one go about and debug this further?
如何进一步调试?
回答by ArtemSky
Try laravel-corspackage that allows you to send Cross-Origin Resource Sharing headers with Laravel middleware configuration.
尝试使用laravel-cors包,它允许您使用 Laravel 中间件配置发送跨源资源共享标头。
回答by Behzad
First solution
第一个解决方案
Try to set the CORS middleware as a global middleware.
尝试将 CORS 中间件设置为全局中间件。
the handle function
in the CORS middleware
:
在handle function
中CORS middleware
:
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS')
->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
}
to add this middleware globally, go to App\Http\Kernel
, and add this line in the $middleware
array:
要全局添加此中间件,请转到App\Http\Kernel
,并将此行添加到$middleware
数组中:
\App\Http\Middleware\Cors::class,
Second solution
第二种解决方案
you can also add this code in the bootstrap/app.php
您也可以在 bootstrap/app.php
header('Access-Control-Allow-Origin', '*');
header('Access-Control-Allow-Methods', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS');
header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
hope it works!
希望它有效!