{"id":42837683,"url":"https://github.com/bairwell/middleware-cors","last_synced_at":"2026-01-30T11:46:11.952Z","repository":{"id":3244402,"uuid":"48852953","full_name":"bairwell/middleware-cors","owner":"bairwell","description":"PSR-7 compatible middleware to provide CORs facilities.","archived":false,"fork":false,"pushed_at":"2022-08-01T23:33:57.000Z","size":78,"stargazers_count":18,"open_issues_count":4,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-30T03:26:44.001Z","etag":null,"topics":["php","psr7","psr7-middleware"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bairwell.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-31T15:05:14.000Z","updated_at":"2023-11-15T14:19:53.000Z","dependencies_parsed_at":"2022-08-21T03:40:16.191Z","dependency_job_id":null,"html_url":"https://github.com/bairwell/middleware-cors","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/bairwell/middleware-cors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bairwell%2Fmiddleware-cors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bairwell%2Fmiddleware-cors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bairwell%2Fmiddleware-cors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bairwell%2Fmiddleware-cors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bairwell","download_url":"https://codeload.github.com/bairwell/middleware-cors/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bairwell%2Fmiddleware-cors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28911983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-30T08:15:08.179Z","status":"ssl_error","status_checked_at":"2026-01-30T08:14:31.507Z","response_time":66,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","psr7","psr7-middleware"],"created_at":"2026-01-30T11:46:11.009Z","updated_at":"2026-01-30T11:46:11.947Z","avatar_url":"https://github.com/bairwell.png","language":"PHP","funding_links":["https://www.patreon.com/rbairwell"],"categories":[],"sub_categories":[],"readme":"# Bairwell\\Middleware-Cors\n\n[![Latest Stable Version](https://poser.pugx.org/bairwell/middleware-cors/v/stable)](https://packagist.org/packages/bairwell/middleware-cors)\n[![License](https://poser.pugx.org/bairwell/middleware-cors/license)](https://packagist.org/packages/bairwell/middleware-cors)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/8aea9987-e478-4faa-a3fe-728e9423b4e7/mini.png)](https://insight.sensiolabs.com/projects/8aea9987-e478-4faa-a3fe-728e9423b4e7)\n[![Coverage Status](https://coveralls.io/repos/bairwell/middleware-cors/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/bairwell/middleware-cors?branch=master)\n[![Build Status](https://travis-ci.org/bairwell/middleware-cors.svg?branch=master)](https://travis-ci.org/bairwell/middleware-cors)\n[![Total Downloads](https://poser.pugx.org/bairwell/middleware-cors/downloads)](https://packagist.org/packages/bairwell/middleware-cors)\n\nThis is a PHP 7 [Composer](https://getcomposer.org/) compatible library for providing a [PSR-7](http://www.php-fig.org/psr/psr-7/) compatible middleware layer for handling\n\"[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)\" (Cross Origin Request Security/Cross-Origin Http Request/HTTP access control) headers and security.\n\n## What does this library provides over other CORs libraries?\n\n* PHP-7 type declarations.\n* Works as a piece of [PSR-7](http://www.php-fig.org/psr/psr-7/) middleware making it compatible with many frameworks (such as [Slim 3](http://slimframework.com) and [Symfony](http://symfony.com/blog/psr-7-support-in-symfony-is-here))\n* Massively flexibility over configuration settings (most can be strings, arrays or callbacks).\n* Follows the [CORs flowchart](http://www.html5rocks.com/static/images/cors_server_flowchart.png) and actively rejects invalid requests.\n* Only sends the appropriate headers when necessary.\n* On CORs \"OPTIONS\" request, ensure a blank page 204 \"No Content\" page is returned instead of returning unwanted content bodies.\n* Supports [PSR-3](http://www.php-fig.org/psr/psr-3/) based loggers for debugging purposes.\n* Ignores non-CORs \"OPTIONS\" requests (for example, on REST services). A CORs request is indicated by the presence of the Origin: header on the inbound request.\n* Fully unit tested.\n* Licensed under the [MIT License](https://opensource.org/licenses/MIT) allowing you to practically do whatever you want.\n* Uses namespaces and is 100% object orientated.\n* Blocks invalid settings.\n* Minimal third party requirements (just the definition files \"[psr/http-message](https://github.com/php-fig/http-message)\" and \"[psr/log](https://github.com/php-fig/log)\" as interface definitions, and [PHPUnit](https://phpunit.de/), [PHPCodeSniffer](http://www.squizlabs.com/php-codesniffer), and [Monolog](https://github.com/Seldaek/monolog) for development/testing).\n\n# Installation\nInstall the latest version with Composer via:\n\n```bash\n$ composer require bairwell/middleware-cors\n```\n\nor by modifying your `composer.json` file:\n````\n{\n  \"require\": {\n    \"bairwell/middleware-cors\": \"@stable\"\n  }\n}\n````\n\nor from the Github repository (which is needed to be able to fork and contribute):\n````\n$ git clone git://github.com:bairwell/middleware-cors.git\n````\n\n# Usage\n\nYou can utilise this CORs library as simply as:\n\n```php\n$slim = new \\Slim\\App(); // use Slim3 as it supports PSR7 middleware\n\n// add CORs\n$slim-\u003eadd(new MiddlewareCors());\n\n// add routes\n$slim-\u003erun(); // get Slim running\n```\n\nbut that won't really add much (as it allows all hosts origin and methods by default).\n\nYou can make it slightly more complex by:\n\n```php\n$slim = new \\Slim\\App(); // use Slim3 as it supports PSR7 middleware\n\n$config = [\n    'origin' =\u003e '*.example.com' // allow all hosts ending example.com\n];\n\n// add CORs\n$slim-\u003eadd(new MiddlewareCors($config));\n\n// add routes\n$slim-\u003erun(); // get Slim running\n```\n\nor\n\n```php\n$slim = new \\Slim\\App(); // use Slim3 as it supports PSR7 middleware\n\n$config = [\n    'origin' =\u003e ['*.example.com', '*.example.com.test', 'example.com', 'dev.*'],\n    'allowCredentials' =\u003e true\n];\n\n$slim-\u003eadd(new MiddlewareCors($config)); // add CORs\n\n// add routes\n$slim-\u003erun(); // get Slim running\n```\n\nwhich will allow all Origins ending .example.com or *.example.com.test, the exact example.com origin or\nany host starting with dev. It'll also allow credentials to be allowed.\n\nFor a more complicated integration which relies on the Slim router to feed back which methods are actually\nallowed per route, see ``tests/MiddlewareCors/FunctionalTests/SlimTest.php``\n\n## Suggested settings\n```php\n// read the allowed methods for a route\n$corsAllowedMethods = function (ServerRequestInterface $request) use ($container) : array {\n    // if this closure is called, make sure it has the route available in the container.\n    /* @var RouterInterface $router */\n    $router = $container-\u003eget('router');\n\n    $routeInfo = $router-\u003edispatch($request);\n    $methods = [];\n    // was the method called allowed?\n    if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {\n        $methods = $routeInfo[1];\n    } else {\n        // if it was, see if we can get the routes and then the methods from it.\n        // @var \\Slim\\Route $route\n        $route = $request-\u003egetAttribute('route');\n        \n        // has the request get a route defined? is so use that\n        if (null !== $route) {\n            $methods = $route-\u003egetMethods();\n        }\n    }\n\n    // if we have methods, let's list them removing the OPTIONs one.\n    if (0 === count($methods)) {\n        // find the OPTIONs method\n        $key = array_search('OPTIONS', $methods,true);\n        // and remove it if set.\n        if (false !== $key) {\n            unset($methods[$key]);\n            $methods = array_values($methods);\n        }\n    }\n\n    return $methods;\n};\n\n$cors = new MiddlewareCors([\n    'origin' =\u003e ['*.example.com','example.com','*.example.com.test','192.168.*','10.*'],\n    'exposeHeaders' =\u003e '',\n    'maxAge' =\u003e 120,\n    'allowCredentials' =\u003e true,\n    'allowMethods' =\u003e $corsAllowedMethods,\n    'allowHeaders' =\u003e ['Accept', 'Accept-Language', 'Authorization', 'Content-Type','DNT','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Origin'],\n]);\n\n$slim-\u003eadd($cors);\n```\n## Standards\n\nThe following [PHP FIG](http://www.php-fig.org/psr/) standards should be followed:\n\n * [PSR 1 - Basic Coding Standard](http://www.php-fig.org/psr/psr-1/)\n * [PSR 2 - Coding Style Guide](http://www.php-fig.org/psr/psr-2/)\n * [PSR 3 - Logger Interface](http://www.php-fig.org/psr/psr-3/)\n * [PSR 4 - Autoloading Standard](http://www.php-fig.org/psr/psr-4/)\n * [PSR 5 - PHPDoc Standard](https://github.com/phpDocumentor/fig-standards/tree/master/proposed) - (still in draft)\n * [PSR 7 - HTTP Message Interface](http://www.php-fig.org/psr/psr-7/) \n * [PSR 12 - Extended Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md) - (still in draft)\n \n### Standards Checking\n[PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer/) highlights potential coding standards issues.\n\n`vendor/bin/phpcs`\n\nPHP CS will use the configuration in `phpcs.xml.dist` by default.\n\nTo see which sniffs are running add \"-s\"\n\n## Unit Tests\n[PHPUnit](http://phpunit.de) is installed for unit testing (tests are in `tests`)\n\nTo run unit tests:\n`vendor/bin/phpunit`\n\nFor a list of the tests that have ran:\n`vendor/bin/phpunit --tap`\n\nTo restrict the tests run:\n`vendor/bin/phpunit --filter 'MiddlewareCors\\\\Exceptions\\\\BadOrigin'`\n\nor just\n\n`vendor/bin/phpunit --filter 'ExceptionTest'`\n\nfor all tests which have \"Exception\" in them and:\n`vendor/bin/phpunit --filter '(ExceptionTest::testEverything|ExceptionTest::testStub)'`\n\nto test the two testEverything and testStub methods in the ExceptionTest class (for example).\n\n# Licence/License\n\nLicenced under the MIT license. See LICENSE.md for full information.\n\nBairwell/MiddlewareCors is Copyright (c) Bairwell Ltd/Richard Bairwell 2016.\n\n# Supporting development\n\nYou can help support development of this library via a variety of methods:\n * \"Sponsorship\" via a monthly donation via [Patreon](https://www.patreon.com/rbairwell)\n * [Reporting issues](https://github.com/bairwell/middleware-cors/issues)\n * Making updates via [Github](https://github.com/bairwell/middleware-cors)\n * Spreading the word.\n * Just letting me know what you think of it via [Twitter](http://twitter.com/rbairwell) or via [Bairwell Ltd](http://www.bairwell.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbairwell%2Fmiddleware-cors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbairwell%2Fmiddleware-cors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbairwell%2Fmiddleware-cors/lists"}