jzaaa /

此包的最新版本(v3.0.3)没有可用的许可信息。

CakePHP 4 插件,用于在您的应用程序中激活 cors 域

维护者

详细信息

github.com/JZaaa/cakephp-cors

源代码

安装: 12

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 31

类型:cakephp-plugin

v3.0.3 2023-08-24 05:54 UTC

This package is auto-updated.

Last update: 2024-10-01 00:09:17 UTC


README

Build Status

一个用于在您的应用程序中使用中间件激活 cors 域的 CakePHP (4+) 插件。

了解 CORS 的更多信息

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

要求

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

安装

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

安装 composer 包的推荐方法是

composer require jzaaa/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
]

阅读更多