ServerInfo and request metadata
GT\Http\ServerInfo wraps the raw $_SERVER array in a readable, predictable object.
This is useful when you need request metadata, but you do not want server variables spread across the whole codebase.
Why not just read $_SERVER directly?
You can, but the drawbacks add up quickly:
- the keys are all-caps strings
- some values may be missing
- related URI and query work is left to you
- the code becomes tightly coupled to PHP superglobals
ServerInfo gives us one place to read these values through named methods instead.
[!NOTE] In WebEngine, reading from
$_SERVERdirectly is prohibited by Protected Globals, but theServerInfoobject is constructed automatically, available in the Service Container.
Construct it
use GT\Http\ServerInfo;
$serverInfo = new ServerInfo($_SERVER);
Common reads
echo $serverInfo->getRequestMethod();
echo $serverInfo->getServerProtocol();
echo $serverInfo->getRemoteAddress();
echo $serverInfo->getDocumentRoot();
Headers from $_SERVER
getHttpHeadersArray() extracts the HTTP_* values and converts them into header-style names.
$headers = $serverInfo->getHttpHeadersArray();
echo $headers["ACCEPT"] ?? "";
Query-string helpers
$queryString = $serverInfo->getQueryString();
$queryParams = $serverInfo->getQueryParams();
$updated = $serverInfo->withQueryParams([
"page" => "2",
"sort" => "name",
]);
Like the other immutable-style helpers in this package, withQueryString() and withQueryParams() return a clone.
URI helpers
There are two URI-oriented methods:
getRequestUri()for the request target URIgetFullUri()for a fuller absolute URI view
echo $serverInfo->getRequestUri();
echo $serverInfo->getFullUri();
These are especially handy when reconstructing absolute URLs from server state.
HTTPS and ports
if($serverInfo->isHttps()) {
// secure request
}
$port = $serverInfo->getServerPort();
The class also accounts for cases where the host header does not explicitly include the port.
Nullable values
Some server fields are optional in PHP, so methods such as these may return null:
getServerName()getServerSoftware()getAuthUser()getAuthPassword()getRemotePort()getRequestScheme()
That makes it easier to handle incomplete server environments safely.
[!NOTE] WebEngine uses HTTP
Requestobjects for most day-to-day work, butServerInfostill fits naturally anywhere you need a clear wrapper around raw server metadata, especially in tests or lower-level infrastructure code.
Another factory in this package helps choose response classes from the request’s Accept header. That is covered in ResponseFactory and content negotiation.