oihana / php-middleware
Composable PHP middleware helpers โ security headers (HSTS, CSP, X-Frame-Options, Referrer-Policy, X-Content-Type-Options), CORS with preflight, CSRF, request-id, maintenance mode, rate limiting. PSR-7 compatible, zero magic strings.
Requires
- php: >=8.4
- oihana/php-enums: dev-main
- oihana/php-http: dev-main
- psr/http-message: ^2.0
Requires (Dev)
- nunomaduro/collision: ^8.8
- phpdocumentor/shim: ^3.8
- phpunit/phpunit: ^12
- slim/psr7: ^1.7
Suggests
- oihana/php-memcached: Provides MemcachedRateLimitStore (production-grade backend for enforceRateLimit()).
README
Composable PHP HTTP middleware helpers. Part of the Oihana PHP ecosystem, this package ships procedural helpers to build typed security-headers responses and apply CORS with preflight handling โ PSR-7 compatible, zero magic strings.
๐ Documentation
Full API reference (generated with phpDocumentor): https://bcommebois.github.io/oihana-php-middleware
User guides (FR + EN) live under wiki/.
๐ฆ Installation
Requires PHP 8.4+. Install via Composer:
composer require oihana/php-middleware
โจ What you can do
Security headers
withSecurityHeaders()โ single helper to apply HSTS,Content-Security-Policy,X-Frame-Options,Referrer-Policy,X-Content-Type-Optionsto a PSR-7Responsein one call. Typed values viaReferrerPolicyandFrameOptionsenums โ no magic strings.buildCspHeader()โ compose aContent-Security-Policyvalue from an associative array of directives.CspDirectiveenum exposes the canonical directive names.
CORS
applyCorsHeaders()โ origin allowlist with configurable methods, headers, exposed headers, credentials and max-age. Handles the preflightOPTIONSrequest automatically. Defensive defaults: no*whencredentials = true,Vary: Originadded when the allowlist is dynamic.
Under the hood
- Pure PSR-7 โ no framework lock-in. Works with Slim, Laravel, Symfony HTTP Foundation (via PSR-7 bridge), Hyperf, RoadRunner, etc.
- Built on
oihana/php-httpprimitives (isHttpsRequest, etc.) andoihana/php-enumstyped HTTP header constants.
โ Running tests
Run all tests:
composer test
๐ ๏ธ Generate the documentation
composer doc
๐งพ License
Licensed under the Mozilla Public License 2.0 (MPLโ2.0).
๐ค About the author
- Author: Marc ALCARAZ (aka eKameleon)
- Email:
marc@ooop.fr - Website:
https://www.ooop.fr
๐ Related packages
| Package | Description |
|---|---|
oihana/php-http |
Composable PHP HTTP primitives (client IP detection, signed URLs, cookies, content negotiation, โฆ) consumed by this library. |
oihana/php-enums |
Typed HTTP constants (HttpHeader, โฆ). |
