composer require proteins/response
Require the class via :
use Proteins\Response;Append a string to the response buffer via the add method.
Response::add('Hello world!');The type method accepts a MIME type string (or a Response::TYPE_* constant) for the body content type.
Response::send();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
$response_headers = Response::headers();$response_body = Response::body();You can set the entire response body by passing a parameter to the body method.
Response::body($new_body);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.errorevent.
Response::error(401, "User not authorized");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")
]);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 :
| Extensions | Type |
|---|---|
js |
script |
css |
style |
woff,woff2,ttf,eof |
font |
png,svg,gif,jpg |
image |
| other | text |
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',
],
]);