wskscorpz/cake-cors

此软件包最新版本(v1.0.0)没有提供许可信息。

CakePHP (3.3.x) 插件,用于在您的应用程序中激活 cors 域

安装: 5

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:cakephp-plugin

v1.0.0 2024-02-01 13:59 UTC

This package is auto-updated.

Last update: 2024-09-30 16:28:38 UTC


README

Build Status

A CakePHP (4+) 插件,用于在您的应用程序中使用 中间件 激活 cors 域。此插件已更新到最新 PR,可供使用。

了解更多关于 CORS 的信息

对于 cake 3.3+,请使用分支 cake-3

要求

  • PHP 版本 7.2 或更高
  • CakePhp 4.0 或更高

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 软件包的推荐方法是

composer require ozee31/cakephp-cors

快速入门

加载插件

// In src/Application.php
public function bootstrap(): void
{
    // code ...
    $this->addPlugin('Cors');
}

默认情况下,插件授权 cors 对所有来源、所有方法和所有头进行缓存,并缓存一天。

配置

默认配置

<?php
[
    'AllowOrigin' => true, // accept all origin
    'AllowCredentials' => true,
    'AllowMethods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], // accept all HTTP methods
    'AllowHeaders' => true, // accept all headers
    'ExposeHeaders' => false, // don't accept personal headers
    'MaxAge' => 86400, // cache for 1 day
    'exceptionRenderer' => 'Cors\Error\AppExceptionRenderer', // Use ExeptionRenderer class of plugin

更改配置

app.php 中添加

'Cors' => [
    // My Config
]

AllowOrigin (Access-Control-Allow-Origin)

返回的资源可能有一个 Access-Control-Allow-Origin 头,其语法如下

'Cors' => [
    // Accept all origins
    'AllowOrigin' => true,
    // OR
    'AllowOrigin' => '*',

    // Accept one origin
    'AllowOrigin' => 'http://flavienbeninca.fr'

    // Accept many origins
    'AllowOrigin' => ['http://flavienbeninca.fr', 'http://google.com']
]

AllowCredentials (Access-Control-Allow-Credentials)

Access-Control-Allow-Credentials 头指示当 credentials 标志为 true 时,响应是否可以被暴露。当作为对预请求响应的一部分时,这表明是否可以使用凭证进行实际请求。注意,简单的 GET 请求不会被预请求,因此如果请求带有凭证的资源,如果此头不在资源中返回,则浏览器会忽略此响应,并将其返回给 Web 内容。

'Cors' => [
    'AllowCredentials' => true,
    // OR
    'AllowCredentials' => false,
]

AllowMethods (Access-Control-Allow-Methods)

'Cors' => [
    // string
    'AllowMethods' => 'POST',
    // OR array
    'AllowMethods' => ['GET', 'POST'],
]

AllowHeaders (Access-Control-Allow-Headers)

Access-Control-Allow-Headers 头用于对预请求响应,指示在发送实际请求时可以使用哪些 HTTP 头。

'Cors' => [
    // accept all headers
    'AllowHeaders' => true,

    // accept just authorization
    'AllowHeaders' => 'authorization',

    // accept many headers
    'AllowHeaders' => ['authorization', 'other-header'],
]

ExposeHeaders (Access-Control-Expose-Headers)

Access-Control-Expose-Headers 头允许服务器将浏览器允许访问的标题列入白名单。例如

'Cors' => [
    // nothing
    'ExposeHeaders' => false,

    // string
    'ExposeHeaders' => 'X-My-Custom-Header',

    // array
    'ExposeHeaders' => ['X-My-Custom-Header', 'X-Another-Custom-Header'],
]

MaxAge (Access-Control-Max-Age)

Access-Control-Max-Age 头指示预请求请求的结果可以缓存多长时间。例如,请参阅上面的示例。

'Cors' => [
    // no cache
    'MaxAge' => false,

    // 1 hour
    'MaxAge' => 3600,

    // 1 day
    'MaxAge' => 86400,
]

exceptionRenderer

此选项将 app.php 中的默认 exceptionRenderer 覆盖。

默认情况下,此类从 Error.exceptionRenderer 继承以添加 Cors 头

如果您不想覆盖 exceptionRenderer,您必须编写

'Cors' => [
	'exceptionRenderer' => false
]

阅读更多