resultsystems / laravel-cors
2.0.0
2018-02-15 12:54 UTC
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2021-05-30 12:26:14 UTC
README
-
此包已被废弃,不再维护。作者建议使用https://github.com/fruitcake/laravel-cors包。
-
感谢超过6,000次安装
包已被废弃,因为Laravel 7.X已支持原生CORS,仅当您使用6.X或更低版本时使用。
安装
1. 依赖
使用 composer,执行以下命令自动安装 composer.json
composer require resultsystems/laravel-cors
2. 中间件
要使用它们,需要将它们注册到您的 app/Http/Kernel.php 文件中。
protected $middleware = [ // other middleware ommited \ResultSystems\Cors\CorsMiddleware::class, ];
3. 提供者(可选)
在您的Laravel应用程序中,选择允许的域名,需要在您的 config/app.php 文件中注册包。在 providers 部分末尾添加以下代码
// file START ommited 'providers' => [ // other providers ommited \ResultSystems\Cors\CorsServiceProvider::class, ], // file END ommited
3.1 公开配置文件(仅当您已执行步骤3时)
要公开随包提供的默认配置文件,请执行以下命令
php artisan vendor:publish --provider="ResultSystems\Cors\CorsServiceProvider"
4 配置(仅当您已执行步骤3和3.1时)
使用允许的域名配置文件
config/cors.php
受http://en.vedovelli.com.br/2015/web-development/Laravel-5-1-enable-CORS/启发。感谢 @vedovelli
5 奖励
如果您使用 nginx
将这些配置添加到网站配置文件中
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /$is_args$args;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Authorization,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
如果是apache,可能需要将以下行添加到 .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
</IfModule>