Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

CorsMiddleware
in package
implements MiddlewareInterface

CORS Middleware

Handles Cross-Origin Resource Sharing headers and OPTIONS preflight requests.

Origin resolution order:

  1. Constructor $origins argument (if not null)
  2. ZEALPHP_CORS_ORIGINS env var (comma-separated)
  3. Falls back to ['*'] with a one-time warning logged via elog()

Wildcard (*) is a security foot-gun for any API serving credentials or user-scoped data; the warning surfaces this without breaking existing apps. Lock down origins explicitly in production:

$app->addMiddleware(new \ZealPHP\Middleware\CorsMiddleware( origins: ['https://myapp.com'], methods: ['GET', 'POST', 'PUT', 'DELETE'], headers: ['Content-Type', 'Authorization'], credentials: true, maxAge: 3600, ));

Or, to lock down without touching code:

ZEALPHP_CORS_ORIGINS="https://myapp.com,https://admin.myapp.com" php app.php

Table of Contents

Interfaces

MiddlewareInterface

Properties

$credentials  : bool
$headers  : array<int, string>
$maxAge  : int
$methods  : array<int, string>
$origins  : array<int, string>
$warnedWildcard  : bool

Methods

__construct()  : mixed
process()  : ResponseInterface
resolveOrigin()  : string
resolveOriginsList()  : array<int, string>

Properties

Methods

__construct()

public __construct([array<int, string>|null $origins = null ][, array<int, string> $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] ][, array<int, string> $headers = ['Content-Type', 'Authorization', 'X-Requested-With', 'Accept'] ][, bool $credentials = false ][, int $maxAge = 86400 ]) : mixed
Parameters
$origins : array<int, string>|null = null

Explicit allowed origins, or null to fall back to env / wildcard.

$methods : array<int, string> = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
$headers : array<int, string> = ['Content-Type', 'Authorization', 'X-Requested-With', 'Accept']
$credentials : bool = false
$maxAge : int = 86400

process()

public process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
$request : ServerRequestInterface
$handler : RequestHandlerInterface
Return values
ResponseInterface

resolveOrigin()

private resolveOrigin(string $requestOrigin) : string
Parameters
$requestOrigin : string
Return values
string

resolveOriginsList()

private resolveOriginsList(array<int, string>|null $explicit) : array<int, string>
Parameters
$explicit : array<int, string>|null
Return values
array<int, string>
On this page