proteins/response

Handles the HTTP Response for the current execution

1.0.5 2019-05-30 08:13 UTC

This package is auto-updated.

Last update: 2024-10-29 05:23:04 UTC


README

Protein | Response

The response module wrap and handles the payload sended to the request agent.

Install

composer require proteins/response

Require the class via :

use Proteins\Response;

Appending data to the response

Append a string to the response buffer via the add method.

Response::add('Hello world!');

Changing the content type

The type method accepts a MIME type string (or a Response::TYPE_* constant) for the body content type.

Response::send();

Adding an header to the response

The header($name, $value) method set an header for being sended to the request agent.

Response::header('Authorization','Bearer mF_9.B5f-4.1JqM');
Authorization: Bearer mF_9.B5f-4.1JqM

Get all defined headers

$response_headers = Response::headers();

Get response body

$response_body = Response::body();

Set the entire response body

You can set the entire response body by passing a parameter to the body method.

Response::body($new_body);

Set the HTTP response status

You can set the HTTP response status with the status method.

Response::status(401);

The error($code, $message='') method is used to pass errors.

This method triggers the core.response.error event.

Response::error(401, "User not authorized");

Force download of response body

You can force download of response body with download method passing filename as parameter. Pass a falsy value to disable download.

Response::download("export.csv");

Download method support also array as parameter with raw string data.

Response::download([
	"filename" 	=> "export.csv",
	"charset" 	=> "utf-8",
	"mime" 		=> "text/csv",
	"body" 		=> CSV::fromSQL("SELECT * FROM users")
]);

HTTP/2 Server Push

The HTTP/Server Push support is enabled via the push method.
If you have a list of resource links to be pushed in the next Response::send you can pass the URI and the resource type as defined in the W3C Preload Draft

Response::push('/assets/css/main.css');
Response::push('/assets/js/main.js');

If you don't pass resource type as a second parameter the code will be guess from the extension, however is better (faster) to specify the resource type (for the as parameter of the preload header format).

The current auto-discovered resource types are :

Response::push('/assets/css/main.css','style');
Response::push('/assets/js/main.js','script');

Multiple resources can be passed to a single push call via an array :

Response::push([
  '/assets/css/main.css',
  '/assets/js/main.js',
]);

and as the same as the direct call version you can define resource types via array-keys :

Response::push([
  'style'  => '/assets/css/main.css',
  'script' => [
    '/assets/js/vendors.js',
    '/assets/js/main.js',
  ],
]);