HTTP Response
In addition to the native PHP features, there are also additional specific features to make it easier to handle HTTP response.
Set or remove response headers
The Response::header() is similar to native header() function, but he checks for issues in values. Example:
use Inphinit\Http\Response;
Response::header('Foo', 'bar');
Set or remove Content-Type header
Setting Content-Type as application/xml:
use Inphinit\Http\Response;
Response::type('application/xml');
Setting Content-Type as text/css with iso-8859-1 charset:
use Inphinit\Http\Response;
Response::type('text/csv', 'iso-8859-1');
Some types have shortcuts:
| Usage | Equivalent |
|---|---|
Response::type('bson') |
Response::type('application/bson') |
Response::type('json') |
Response::type('application/json') |
Response::type('xml') |
Response::type('application/xml') |
Response::type('atom') |
Response::type('application/atom+xml') |
Response::type('rss') |
Response::type('application/rss+xml') |
Force download
Forcing current page download:
use Inphinit\Http\Response;
Response::download('page.html');
Setting the Content-Length can help download managers provide an estimated download completion time, which can be done by setting the second parameter:
use Inphinit\Http\Response;
Response::download('report.pdf', 1000);
Status code
Get current response status code:
use Inphinit\Http\Response;
echo Response::status();
Set response status code:
use Inphinit\Http\Response;
Response::status(201);
Set response status code and return previous code:
use Inphinit\Http\Response;
$previous = Response::status(204);
echo $previous;
It is possible to use the constants available in Inphinit\Http\Status (see: Response Codes):
use Inphinit\Http\Response;
use Inphinit\Http\Status;
// Bad Request
Response::status(Status::HTTP_BAD_REQUEST);
Set cache or no-cache headers
Define cache in seconds
Cache expires in 3600 seconds:
use Inphinit\Http\Response;
Response::cache(3600);
No cache
Use -1 for use no cache:
use Inphinit\Http\Response;
Response::cache(-1);
Set cache with English textual datetime descriptions
Cache expires 1 week:
use Inphinit\Http\Response;
Response::cache('+1 week');
More details in https://www.php.net/manual/en/datetime.formats.php#datetime.formats.relative.