ctw / ctw-middleware-generatedat
This PSR-15 middleware adds an X-Generated-At timestamp to the Response header.
Installs: 19
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/ctw/ctw-middleware-generatedat
Requires
- php: ^8.3
- ctw/ctw-middleware: ^4.0
- psr/container: ^1.0 || ^2.0
Requires (Dev)
- ctw/ctw-qa: ^5.0
- phpunit/phpunit: ^12.0
- symfony/var-dumper: ^7.0
README
PSR-15 middleware that adds an X-Generated-At header with an ISO 8601 UTC timestamp to every response.
Introduction
Why This Library Exists
Knowing when a response was generated is valuable for debugging, monitoring, and cache validation. While HTTP provides standard caching headers like Date and Last-Modified, these are often set by web servers or proxies rather than the application itself.
The X-Generated-At header provides an application-level timestamp indicating exactly when your PHP code generated the response:
- Debugging: Quickly identify if you're seeing a cached response or a freshly generated one
- Performance monitoring: Compare generation timestamps with response receipt times
- Cache verification: Confirm that CDN or browser caches are serving expected content
- Audit trails: Log when specific responses were generated for compliance purposes
Problems This Library Solves
- Cache ambiguity: Difficult to determine if a response came from cache or was freshly generated
- Timezone confusion: Server timestamps may use local time; this middleware uses consistent UTC format
- Missing application timestamps: Web server
Dateheaders don't reflect when application code executed - Debugging complexity: Without timestamps, correlating logs with responses requires additional tooling
- Inconsistent formatting: Custom timestamp implementations vary; this provides ISO 8601 standard format
Where to Use This Library
- Development environments: Verify that code changes are being reflected in responses
- Production applications: Monitor response freshness and debug caching issues
- API services: Provide clients with precise response generation timestamps
- Multi-tier architectures: Distinguish between application, CDN, and proxy caching
- Debugging sessions: Correlate browser responses with server-side logs
Design Goals
- ISO 8601 format: Uses standard
Y-m-d\TH:i:s\Zformat for universal parsing - UTC timezone: Eliminates timezone ambiguity with consistent UTC timestamps
- Request-based accuracy: Uses
REQUEST_TIME_FLOATfor precise timing - Minimal overhead: Simple header addition with negligible performance impact
- Non-intrusive: Adds metadata without modifying response content
Requirements
- PHP 8.3 or higher
- ctw/ctw-middleware ^4.0
Installation
Install by adding the package as a Composer requirement:
composer require ctw/ctw-middleware-generatedat
Usage Examples
Basic Pipeline Registration (Mezzio)
use Ctw\Middleware\GeneratedAtMiddleware\GeneratedAtMiddleware; // In config/pipeline.php or similar $app->pipe(GeneratedAtMiddleware::class);
Response Header Output
HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 X-Generated-At: 2024-01-15T14:30:45Z
Inspecting with cURL
curl -I https://example.com/ # Response includes: # X-Generated-At: 2024-01-15T14:30:45Z
Inspecting in Browser DevTools
- Open Developer Tools (F12)
- Navigate to the Network tab
- Select a request
- View Response Headers
- Look for
X-Generated-At
ConfigProvider Registration
The package includes a ConfigProvider for automatic factory registration:
// config/config.php return [ // ... \Ctw\Middleware\GeneratedAtMiddleware\ConfigProvider::class, ];
Header Format
| Component | Value | Description |
|---|---|---|
| Header name | X-Generated-At |
Custom header indicating generation time |
| Format | ISO 8601 | Y-m-d\TH:i:s\Z |
| Timezone | UTC | Indicated by Z suffix |
| Example | 2024-01-15T14:30:45Z |
January 15, 2024 at 14:30:45 UTC |