Download the PHP package bairwell/middleware-cors without Composer
On this page you can find all versions of the php package bairwell/middleware-cors. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bairwell/middleware-cors
More information about bairwell/middleware-cors
Files in bairwell/middleware-cors
Package middleware-cors
Short Description A PSR-7 middleware layer for providing CORS (Cross Origin Request Security) headers and security provisions. Instead of just allowing invalid CORs requests to come through, this middleware actively blocks them after validating.
License MIT
Homepage https://github.com/bairwell/middleware-cors
Informations about the package middleware-cors
Bairwell\Middleware-Cors
This is a PHP 7 Composer compatible library for providing a PSR-7 compatible middleware layer for handling "CORS" (Cross Origin Request Security/Cross-Origin Http Request/HTTP access control) headers and security.
What does this library provides over other CORs libraries?
- PHP-7 type declarations.
- Works as a piece of PSR-7 middleware making it compatible with many frameworks (such as Slim 3 and Symfony)
- Massively flexibility over configuration settings (most can be strings, arrays or callbacks).
- Follows the CORs flowchart and actively rejects invalid requests.
- Only sends the appropriate headers when necessary.
- On CORs "OPTIONS" request, ensure a blank page 204 "No Content" page is returned instead of returning unwanted content bodies.
- Supports PSR-3 based loggers for debugging purposes.
- 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.
- Fully unit tested.
- Licensed under the MIT License allowing you to practically do whatever you want.
- Uses namespaces and is 100% object orientated.
- Blocks invalid settings.
- Minimal third party requirements (just the definition files "psr/http-message" and "psr/log" as interface definitions, and PHPUnit, PHPCodeSniffer, and Monolog for development/testing).
Installation
Install the latest version with Composer via:
or by modifying your composer.json
file:
`
or from the Github repository (which is needed to be able to fork and contribute): `
Usage
You can utilise this CORs library as simply as:
but that won't really add much (as it allows all hosts origin and methods by default).
You can make it slightly more complex by:
or
which will allow all Origins ending .example.com or *.example.com.test, the exact example.com origin or any host starting with dev. It'll also allow credentials to be allowed.
For a more complicated integration which relies on the Slim router to feed back which methods are actually
allowed per route, see tests/MiddlewareCors/FunctionalTests/SlimTest.php
Suggested settings
Standards
The following PHP FIG standards should be followed:
- PSR 1 - Basic Coding Standard
- PSR 2 - Coding Style Guide
- PSR 3 - Logger Interface
- PSR 4 - Autoloading Standard
- PSR 5 - PHPDoc Standard - (still in draft)
- PSR 7 - HTTP Message Interface
- PSR 12 - Extended Coding Style Guide - (still in draft)
Standards Checking
PHP Code Sniffer highlights potential coding standards issues.
vendor/bin/phpcs
PHP CS will use the configuration in phpcs.xml.dist
by default.
To see which sniffs are running add "-s"
Unit Tests
PHPUnit is installed for unit testing (tests are in tests
)
To run unit tests:
vendor/bin/phpunit
For a list of the tests that have ran:
vendor/bin/phpunit --tap
To restrict the tests run:
vendor/bin/phpunit --filter 'MiddlewareCors\\Exceptions\\BadOrigin'
or just
vendor/bin/phpunit --filter 'ExceptionTest'
for all tests which have "Exception" in them and:
vendor/bin/phpunit --filter '(ExceptionTest::testEverything|ExceptionTest::testStub)'
to test the two testEverything and testStub methods in the ExceptionTest class (for example).
Licence/License
Licenced under the MIT license. See LICENSE.md for full information.
Bairwell/MiddlewareCors is Copyright (c) Bairwell Ltd/Richard Bairwell 2016.
Supporting development
You can help support development of this library via a variety of methods:
- "Sponsorship" via a monthly donation via Patreon
- Reporting issues
- Making updates via Github
- Spreading the word.
- Just letting me know what you think of it via Twitter or via Bairwell Ltd