Download the PHP package andrewdyer/cors-response-emitter without Composer
On this page you can find all versions of the php package andrewdyer/cors-response-emitter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andrewdyer/cors-response-emitter
More information about andrewdyer/cors-response-emitter
Files in andrewdyer/cors-response-emitter
Package cors-response-emitter
Short Description A CORS-aware response emitter for Slim Framework applications that applies consistent CORS and cache-control headers
License MIT
Homepage https://github.com/andrewdyer/cors-response-emitter
Informations about the package cors-response-emitter
CORS Response Emitter
A CORS-aware response emitter for Slim Framework applications that applies consistent CORS and cache-control headers.
Introduction
This library applies CORS and cache-control headers when emitting Slim responses. It checks the incoming request origin against an explicit allowlist, returns credentialed CORS responses for trusted origins, and supports wildcard responses for public APIs without credentials. Cache-control headers are applied consistently across emitted responses.
Prerequisites
- PHP: Version 8.3 or higher is required.
- Composer: Dependency management tool for PHP.
- Slim Framework: Version 4 is required.
Installation
Getting Started
The examples below demonstrate how to configure the emitter and emit a Slim response with CORS headers.
1. Configure trusted origins
Provide an allowlist of origins that may receive credentialed CORS responses.
2. Emit the response
After Slim handles the request, pass the response to the emitter.
Usage
The emitter resolves CORS headers from the request origin and allowlist configuration:
| Scenario | Access-Control-Allow-Origin |
Access-Control-Allow-Credentials |
Vary |
|---|---|---|---|
| Request origin matches an explicit allowlist entry | Reflected origin (e.g. https://app.example.com) |
true |
Origin |
"*" in allowlist, no explicit match |
* |
(omitted) | (omitted) |
| No match and no wildcard allowlist entry | (omitted) | (omitted) | (omitted) |
Allow exact origins
Use explicit origins when endpoints need credentialed cross-origin requests.
Allow any origin for public APIs
A wildcard origin ("*") may be configured as an allowlist entry to permit requests from any origin. This is suitable for fully public, unauthenticated APIs:
Combine exact and wildcard origins
Explicit origins and "*" may be combined. An exact match always takes precedence and receives the credentialed response. Requests from any other origin fall back to the uncredentialed wildcard response:
Important: the CORS specification forbids sending Access-Control-Allow-Credentials: true with Access-Control-Allow-Origin: *. If an endpoint requires cookies, HTTP authentication, or client certificates, use explicit origins.
Complete Example
The following example combines Slim setup, request handling, and CORS-aware response emission:
License
Licensed under the MIT license and is free for private or commercial projects.