Jet\Http_Headers

As the name of the class suggests, it is used for working with HTTP headers, more precisely for sending response headers and very often for various redirects and so on.

Method Meaning of
public static response(
int $code,
array $headers = [],
string $custom_response_message=''
): void
Builds and sends an HTTP response. Parameters:
  • $code HTTP return code (200, 404, or any other)
  • $headers Veditable HTTP headers. It is possible to pass an associated field: Http_Headers::response(Http_Headers::CODE_200_OK, [
        
    'Content-Type'=>'text/plain'
        
    'Header-2'=>'value-2'
    ]);
    And it is equally possible to use a plain field: Http_Headers::response(Http_Headers::CODE_200_OK, [
        
    'Content-Type: text/plain'
        
    'Header-2: value-2'
    ]);
  • $custom_response_message Jet automatically adds a textual interpretation of the code to the corresponding HTTP code in the response.

    For example:
    HTTP/1.1 200 OK
    HTTP/1.1 404 Not Found

    These text values can be globally configured via Jet\SysConf_Jet_Http::setResponseMessages
    However, if you want to send your own value in a specific situation (e.g. REST API), you have the option to define it using this parameter.
public static redirect(
int $http_code,
string $target_URL,
array $headers = [],
bool $application_end = true
): void
Redirects to the URL using an optional HTTP return code. For example: Http_Headers::redirect(
    
Http_Headers::CODE_301_MOVED_PERMANENTLY
    
'https://some.domain' 
);

Using the headers parameter it is possible to define additional optional headers (the principle is the same as for the response method).

The $application_end parameter determines whether the application should be terminated after redirection.
public static movedPermanently(
string $target_URL,
array $headers = [],
bool $application_end = true
): void
Identical method as redirect, except that the HTTP code 301 is used automatically.
public static movedTemporary(
string $target_URL,
array $headers = [],
bool $application_end = true
): void
Identical method as redirect, except that the HTTP code 302 is used automatically.
public static reload(
array $set_GET_params = [],
array $unset_GET_params = [],
?string $set_anchor = null,
bool $application_end = true
): void
Probably the most commonly used method of this class. It is used, for example, after submitting a form (most often by the POST method) or generally performing some action.

For example, the user has submitted an article using a form (POST method), the article is added and you would need to redirect the user to the detail (editing) of this newly added article.

In such a situation you would need to redirect (probably) to the same URL, with the difference that, for example, you add a new value to the GET parameters, or on the contrary, you remove a value from the existing parameters.

Let's see some practical examples.

Suppose that the GET parameter delete is to perform the action of deleting an article. The URL can then be:
https://some.domain/armin/articles/?delete=123

Your controller will handle everything, delete the article and needs to get the user to the article list. So to the URL https://some.domain/armin/articles/. He'll do it like this: Http_Headers::reload(unset_GET_params: ['delete']);
And the second example. A new article has been added and you want to send the user directly to edit it, i.e. to the URL https://some.domain/armin/articles/?id=123
Here's how you do it: Http_Headers::reload(set_GET_params: ['id'=>$id]);
public static sendDownloadFileHeaders(
string $file_name,
string $file_mime,
int $file_size,
bool $force_download = false
): void
If you need to send a file to a user (download, etc.), you need to build the HTTP headers correctly. This method will do it for you.

You can use the method on its own, or you can leave everything to the Jet\IO_File::send() method, which just "passes" the file and takes care of everything.
Previous chapter
Jet\Http_Request
Next chapter
Useful helpers for working with various data - Jet\Data