2017 © Pedro Peláez
 

cakephp-plugin cakephp-cors

A CakePHP (3.3.x) plugin for activate cors domain in your application

image

ozee31/cakephp-cors

A CakePHP (3.3.x) plugin for activate cors domain in your application

  • Thursday, July 5, 2018
  • by ozee31
  • Repository
  • 6 Watchers
  • 22 Stars
  • 16,767 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 11 Versions
  • 25 % Grown

The README.md

cakephp-cors

Build Status, (*1)

A CakePHP (4+) plugin for activate cors domain in your application with Middleware., (*2)

Learn more about CORS, (*3)

For cake 3.3+ use branch cake-3, (*4)

Requirements

  • PHP version 7.2 or higher
  • CakePhp 4.0 or higher

Installation

You can install this plugin into your CakePHP application using composer., (*5)

The recommended way to install composer packages is:, (*6)

composer require ozee31/cakephp-cors

Quick Start

Loading the Plugin, (*7)

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

By default the plugin authorize cors for all origins, all methods and all headers and caches all for one day., (*8)

Configuration

Default configuration

<?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

Change config

In app.php add :, (*9)

'Cors' => [
    // My Config
]

AllowOrigin (Access-Control-Allow-Origin)

A returned resource may have one Access-Control-Allow-Origin header, with the following syntax:, (*10)

'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)

The Access-Control-Allow-Credentials header Indicates whether or not the response to the request can be exposed when the credentials flag is true. When used as part of a response to a preflight request, this indicates whether or not the actual request can be made using credentials. Note that simple GET requests are not preflighted, and so if a request is made for a resource with credentials, if this header is not returned with the resource, the response is ignored by the browser and not returned to web content., (*11)

'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)

The Access-Control-Allow-Headers header is used in response to a preflight request to indicate which HTTP headers can be used when making the actual request., (*12)

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

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

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

ExposeHeaders (Access-Control-Expose-Headers)

The Access-Control-Expose-Headers header lets a server whitelist headers that browsers are allowed to access. For example:, (*13)

'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)

The Access-Control-Max-Age header indicates how long the results of a preflight request can be cached. For an example of a preflight request, see the above examples., (*14)

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

    // 1 hour
    'MaxAge' => 3600,

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

exceptionRenderer

This option overload default exceptionRenderer in app.php., (*15)

By default this class extends from Error.exceptionRenderer to add Cors Headers, (*16)

If you don't want to overload exceptionRenderer, You must write, (*17)

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

Read more, (*18)

The Versions

05/07 2018

dev-master

9999999-dev

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

05/07 2018

v1.1.4

1.1.4.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

04/07 2018

v1.1.3

1.1.3.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

29/06 2018

v1.1.2

1.1.2.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

14/08 2017

v1.1.1

1.1.1.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

19/04 2017

v1.1.0

1.1.0.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

13/04 2017

v1.0.4

1.0.4.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

22/03 2017

v1.0.3

1.0.3.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

13/03 2017

v1.0.2

1.0.2.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

01/02 2017

v1.0.1

1.0.1.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires

09/11 2016

v1.0.0

1.0.0.0

A CakePHP (3.3.x) plugin for activate cors domain in your application

  Sources   Download

The Requires

 

The Development Requires