Luminova Framework

View Response

Last updated: 2024-05-24 07:53:21

The View Response class simplifies APs, and WEB content responses within your application's controller class or routing closures. With its global procedural function, you can easily access the class instance without instantiating the class.

  • Class namespace: \Luminova\Template\ViewResponse

Helper Function

The global helper of ViewResponse class will return instance of this class.

<?php
response(int $status = 200)

Parameters:

ParameterTypeDescription
$statusintHTTP status code (default: 200 OK)
$encodeboolEnable content encoding like gzip, deflate.

constructor

Initialize class constructor with HTTP status code .

new ViewResponse(int $status = 200)

Parameters:

ParameterTypeDescription
$statusintHTTP status code (default: 200 OK)

setStatus

Set the HTTP status code using setStatus method.

public setStatus(int $status = 200): self

Parameters:

ParameterTypeDescription
$statusintHTTP status code (default: 200 OK)

Return Value:

ViewResponse - Instance of view response class.


encode

Set enable or disable content encoding, otherwise it will use default flag in env file enable.encoding

public encode(bool $encode): self

Parameters:

ParameterTypeDescription
$encodeboolEnable content encoding like gzip, deflate.

Return Value:

ViewResponse - Instance of view response class.


minify

Set enable or disable content minification, otherwise it will use default flag in env file `page.minification

public minify(bool $minify): self

Parameters:

ParameterTypeDescription
$minifyboolEnable content minification.

Return Value:

ViewResponse - Instance of view response class.


render

Render any content format anywhere with your own customizations.Using this method allows you to pass encode or minify without using the default from env.

public render(mixed $content, $status = 200, array $headers = [],  bool $encode = true, bool $minify = false): int

Parameters:

ParameterTypeDescription
$contentmixedResponse content
$statusintHTTP status code (default: 200 OK)
$headersarrayAdditional headers.
$encodeboolEnable content encoding like gzip.
$minifyboolEnable content minification and compress.

Return Value:

int - Return status code success as STATUS_SUCCESS, otherwise failure as STATUS_ERROR.

This can be used in router or APIs to respond content without an addition overhead in processing the contents.

Note: The default content type is application/json if not was provided in $headers.


json

Send a JSON response.

public json(array|object $content): int

Parameters:

ParameterTypeDescription
$contentarray|objectData to be encoded as JSON

Return Value:

int - Return status code success, otherwise failure.


text

Send a plain text response.

public text(string $content): int

Parameters:

ParameterTypeDescription
$contentstringText content

Return Value:

int - Return status code success, otherwise failure.


html

Send an HTML response.

public html(string $content): int

Parameters:

ParameterTypeDescription
$contentstringHTML content

Return Value:

int - Return status code success, otherwise failure.


xml

Send an XML response.

public xml(string $content): int

Parameters:

ParameterTypeDescription
$contentstringXML content

Return Value:

int - Return status code success, otherwise failure.


download

Download file from server or download response string content.

public download(string $fileOrContent, string|null $name = null, array $headers = []): bool

Parameters:

ParameterTypeDescription
$pathstringPath to the file or content to be downloaded
$namestring|nullOptional name to be used for the downloaded file
$headersarrayOptional download headers.

Return Value:

bool - Return true if the download was successful, false otherwise


redirect

URL redirection, redirect to another URL.

public redirect(string $url = '/', int $response_code): void

Parameters:

ParameterTypeDescription
$urlstringURL location to redirect.
$response_codeintResponse status code