Luminova Framework

PHP Luminova: Management of Incoming HTTP Request Headers

Last updated: 2024-08-29 19:19:12

The header class specifically focuses on managing request headers. It provides methods to interact with and manage request headers easily.

The Header class in Luminova is designed to manage HTTP request headers in a structured and efficient manner, similar to the way the Server class handles server-related data. It provides methods to interact with, retrieve, and manipulate request headers, simplifying the process of managing HTTP requests.


  • Class namespace: \Luminova\Http\Header

Example Usage:

The Header object is available in the Request class object as a public property $request->header.

Get the value for a specific key:

<?php
$value = $header->get('User-Agent');

Set a new value for a key:

<?php
$header->set('User-Agent', 'New User Agent');

Remove a key:

<?php
$header->remove('Accept');

Search for a value:

<?php
$key = $header->search('Host');

Check if a key exists:

<?php
if ($header->has('Accept-Encoding')) {
    // Key exists
}

Count the number of elements:

<?php
$count = $header->count();

Properties

httpMethods

List of all allowed HTTP request methods, which must be in uppercase.

public static string[] $httpMethods = [];

variables

Array containing header variables.

protected array<string,mixed> $variables = [];

Methods

constructor

Initializes the Header object with optional headers. If no headers are passed, it will use the request headers.

public __construct(?array $variables = null): mixed

Parameters:

ParameterTypeDescription
$variables**array<string,mixed>null**Optional key-value pairs of header variables.

get

Retrieves the value of a specific header or all headers if no name is provided.

public get(string|null $name = null, mixed $default = null): mixed|array|string|null

Parameters:

ParameterTypeDescription
$namestring|nullOptional name of the server variable.
$defaultmixedDefault value for the server key.

Return Value:

mixed|array|string|null - Return the value of the specified header, or all headers if $name is null.


set

Sets the value for a specific header.

public set(string $key, mixed $value): void

Parameters:

ParameterTypeDescription
$keystringThe header key to set.
$valuestringThe value to set for the header key.

remove

Removes a specific header by its key.

public remove(string $key): void

Parameters:

ParameterTypeDescription
$keystringThe key of the header to remove.

has

Checks if a specific header key exists.

public has(string $key): bool

Parameters:

ParameterTypeDescription
$keystringThe header key to check.

Return Value:

bool - Return true if the key exists, false otherwise.


count

Gets the total number of header variables.

public count(): int

Return Value:

int - Return the number of header variables.


getHeaders

Extracts all request headers using apache_request_headers or the $_SERVER variables.

public static getHeaders(): array<string,string>

Return Value:

array<string,string> - Return the request headers.


getContentType

Determines the content type based on the file extension and charset.

public static getContentType(string $extension = 'html', string $charset = null): string

Parameters:

ParameterTypeDescription
$extensionstringThe file extension.
$charsetstringThe character set.

Return Value:

string - Return the content type and optional charset.


getContentTypes

Retrieves content types by name.

public static getContentTypes(string $type): array<string,array>

Parameters:

ParameterTypeDescription
$typestringThe type of content types to retrieve.

Return Value:

array<string,array> - Return array, string of content types or null if not found.