The framework's Response service holds global data for forming a response to the client.
When using the framework asynchronously, this data reverts to default values after each request ends.
Methods for assigning data to Response in controllers:
The method is similar for all classes inheriting from Hleb\Base\Container, but forming a response directly in Response outside the controller is considered bad practice.
Example of using Response in application code (the code will also be difficult to maintain in this case):
Access to the Response service can also be obtained through dependency injection via the Hleb\Reference\Interface\Response interface.
To simplify examples, they will only include access via DI from now on.
Combined with print and echo outputs, data from Response will be shown later; the correct strategy is to use only one method for outputting results.
At the end of a request, the framework will still refer to the specified Response object for output, even if this object wasn't returned from the controller. This can be handy for one-time or sequential data addition in Response within a single controller method. If it's necessary to manipulate response objects containing different data, any other Response can be used according to PSR-7. The alternative Response must be returned in the invoked controller method.
The response body consists of data added to the Response object, which can be converted into a string. Typically, this is message text displayed to the user or data in the format of JSON or XML, possibly dynamically generated HTML, etc.
The following methods of the Response service are available for adding data:
set() or setBody() — assigns data, completely overwriting any previous response body if it exists.
add() or addToBody() — appends to the end of the previously added data.
To retrieve data from the service:
get() or getBody() — retrieves the current state of the response body in the Response object.
Before sending data to the client, ensure it is checked for XSS vulnerabilities. If the data has not been processed in this way before, it can be passed through the PHP function htmlspecialchars().
By default, the status is set to 200.
If the response should have a different status, use the setStatus() method, with the first argument being the status and the second a short status message if it differs from the standard.
In the status '404 Not Found', such a message is 'Not Found'.
Standard status messages are usually used, so you can set the status by number directly in the set() method as the second argument.
The method getStatus() allows you to obtain the current HTTP status from the Response service.
Besides the global server-side response headers, you can specify your own headers to be returned with a specific response from the framework. The following methods of the Response service are intended for this second type of headers.
The setHeader() method sets a header by name, overriding the previous value if it was set. In the rare case where multiple identical headers are needed, the replace argument allows adding a header to the current value.
The hasHeader() method checks if a header exists by name.
The getHeader() method is designed to obtain an array of header data by name.
The getHeaders() method returns the data of all headers set in Response as an array.
While operations on headers using standard PHP functions will work in conjunction, conflicts may arise when used together with the Response object. It is better to use just one approach throughout the application.
The default HTTP protocol version is '1.1' unless determined from the current request. Since the return value should usually match the request itself, changes are rarely used.
Nevertheless, the getVersion() and setVersion() methods are available for getting and setting the version respectively.
← Request Caching →