Luminova Framework

Request Header

Last updated: 2024-05-06 17:24:34

The Header class functions similarly to the Server class, but it specifically focuses on managing request headers. It provides methods to interact with and manage request headers in a structured manner.


  • Class namespace: \Luminova\Http\Header

Properties

httpMethods

All allowed HTTP request methods, must be in upper case.

public static array<int,string> $httpMethods
  • This property is static.

variables

Header variables.

protected array $variables

Methods

constructor

Initializes the header constructor.

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

Parameters:

ParameterTypeDescription
$variables?array

get

Get header variables.

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 - The value of the specified server variable, or all server variables if $name is null.


set

Set server variable.

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

Parameters:

ParameterTypeDescription
$keystringThe server variable key to set.
$valuestringThe server variable value.

remove

Removes a server variable by key

public remove(string $key): void

Parameters:

ParameterTypeDescription
$keystringThe key to remove.

has

Check if request header key exist.

public has(string $key): bool

Parameters:

ParameterTypeDescription
$keystringHeader key to check.

Return Value:

bool - Return true if key exists, false otherwise.


count

Get the total number of server variables.

public count(): int

Return Value:

int - Number of server variables


getHeaders

Get all request headers.

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

Return Value:

array<string,string> - The request headers.


getContentType

Get the content type based on 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 - The content type.


getContentTypes

Get content types by name

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

Parameters:

ParameterTypeDescription
$typestringType of content types to retrieve.

Return Value:

array<int,array> - Array of content types or null if not found.


Example Usage:

The header object is available in request class object as public property request()->header

Get value for a specific key.

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

Set a new value for a key.

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

Remove a key.

$header->remove('Accept');

Search for a value

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

Check if a key exists

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

Count the number of elements.

$count = $header->count();