Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/medz/cors

🔮Supported(Laravel/Lumen/PSR-15/Swoft/Slim/ThinkPHP) - PHP CORS (Cross-origin resource sharing) middleware.
https://github.com/medz/cors

cors cors-middleware cors-request crossdomain laravel laravel5 middleware php php-cors psr-15 psr-7 request response slim swoft symfony thinkphp yii2

Last synced: 3 months ago
JSON representation

🔮Supported(Laravel/Lumen/PSR-15/Swoft/Slim/ThinkPHP) - PHP CORS (Cross-origin resource sharing) middleware.

Awesome Lists containing this project

README

        

# CORS

![PHP Composer](https://github.com/medz/cors/workflows/PHP%20Composer/badge.svg)
[![StyleCI](https://styleci.io/repos/125001413/shield?branch=master)](https://styleci.io/repos/125001413)
[![Build Status](https://travis-ci.org/medz/cors.svg?branch=master)](https://travis-ci.org/medz/cors)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmedz%2Fcors.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmedz%2Fcors?ref=badge_shield)

PHP CORS (Cross-origin resource sharing) middleware.

## Support

- [x] [Array, Coding in Native PHP](#array)
- [x] [Using PSR-7](#psr-7)
- [x] [PSR-15 Support](https://github.com/medz/cors/blob/master/docs/psr-15-support.md)
- [ ] Symfony Support
- [x] [Laravel Support](https://github.com/medz/cors/blob/master/docs/using-by-laravel.md)
- [x] [Lumen Support](https://github.com/medz/cors/blob/master/docs/using-by-lumen.md)
- [ ] Yii2 Support
- [x] [Swoft Framework](https://github.com/medz/cors/blob/master/docs/using-by-swoft.md)
- [x] [Slim Framework](https://github.com/medz/cors/blob/master/docs/using-by-slim.md)
- [x] [ThinkPHP Support](https://github.com/medz/cors/blob/master/docs/using-by-thinkphp.md)

## Using

```shell
composer require medz/cors
```

Example:

```php
use Medz\Cors\Cors;

$cors = new Cors($config); // The $config 👉 `config/cors.php` file.
$cors->setRequest($requestType, $request); // The $request is empty array or is `NULL` or $_REQUEST
$cors->setResponse($responseType, $response); // The $response is empty array.
$cors->handle();

$response = $cors->getResponse();
```

Both `$requestType` and `$responseType` are of any type that is individually set to support, but if used in a framework, these two values are usually the same. Because you can set it up individually, you can deliver different values depending on your needs to achieve your goals.

### Configure

The config example:

```php
$config = [
'allow-credentials' => false, // set "Access-Control-Allow-Credentials" 👉 string "false" or "true".
'allow-headers' => ['*'], // ex: Content-Type, Accept, X-Requested-With
'expose-headers' => [],
'origins' => ['*'], // ex: http://localhost
'methods' => ['*'], // ex: GET, POST, PUT, PATCH, DELETE
'max-age' => 0,
];
```

### Array

```php
use Medz\Cors\Cors;

$cors = new Cors($config);
$cors->setRequest('array', $request);
$cors->setResponse('array', $response);
$cors->handle();

$responseHeadersArray = $cors->getResponse();
```

### PSR-7

```php
use Medz\Cors\Cors;

$cors = new Cors($config);
$cors->setRequest('psr-7', $request);
$cors->setResponse('psr-7', $response);

$response = $cors->getResponse();
```

### Other

Because of the interface features provided by this package, you can implement it in a small amount of code in any other framework.

### Excellent Repositories

- [ThinkSNS+](https://github.com/slimkit/thinksns-plus) A strong community system

## License

The component is open-sourced software licensed under the [MIT license](https://github.com/medz/cors/blob/master/LICENSE).

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmedz%2Fcors.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmedz%2Fcors?ref=badge_large)