Phalcon\Http\Message\Response is a representation of an outgoing, server-side response. As per the HTTP specification, this interface includes properties for each of the following:
Status code and reason phrase
NOTE: In the examples below, $httpClient is the client of your choice which implements PSR-7.
<?phpusePhalcon\Http\Message\Response;$response=newResponse();$payload=['code'=>2000,'status'=>'success','payload'=>['id'=>12345,'name'=>'John Doe',]];$response=$response->withHeader('Content-Type','application/json')->withBody(json_encode($payload))->withStatusCode(200);$result=$httpClient->send($response);$payload='The above copyright notice and this permission '.'notice shall be included in all copies or '.'substantial portions of the Software.';$response=$response->withHeader('Content-Type','text/html')->withBody($payload)->withStatusCode(200);$result=$httpClient->send($response);
We are creating a new Phalcon\Http\Message\Response object with a payload represented as JSON, the necessary headers and the status code. The client then sends the response back using the request object.
The above example can be implemented by only using the constructor parameters:
The constructor accepts parameters allowing you to create the object with certain properties populated. You can define the body, status code as well as the headers. All parameters are optional.
body - It defaults to php://memory. The method accepts either an object that implements the StreamInterface interface or a string such as the name of the stream. The default mode for the stream is w+b. If a non valid stream is passed, an InvalidArgumentException is thrown
code - An integer representing the status code for the response. It defaults to 200.
headers - A key value array, with key as the header name and value as the header value.
Returns the body as a StreamInterface object. This method is useful also when in need to quickly adjust the body of the message. You can check the methods you can call by checking the Phalcon\Http\Message\Stream class.
Returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma. If the string parameter representing the header name requested, an empty string is returned.
Returns an array with all the message header values. The keys represent the header name as it will be sent over the wire, and each value is an array of strings associated with the header. While header names are not case-sensitive, this method preserves the exact case in which headers were originally specified.
Returns the response reason phrase associated with the status code. Because a reason phrase is not a required element in a response status line, the reason phrase value may be empty. The return string comes from the IANA HTTP Status Code Registry, as defined in the RFC 7231.
The Request object is immutable. However there are a number of methods that allow you to inject data into it. The returned object is a clone of the original one.
Returns an instance with an additional header appended with the given value. Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added. Throws InvalidArgumentException for invalid header names or values. The header values can be a string or an array of strings.
Returns an instance with the provided value replacing the specified header. While header names are case-insensitive, the casing of the header will be preserved by this function, and returned from getHeaders(). Throws InvalidArgumentException for invalid header names or values.
Return an instance with the specified status code and, optionally, reason phrase. If no reason is defined, the reason string will come from the IANA HTTP Status Code Registry, as defined in the RFC 7231.
Returns an instance with the provided UriInterface URI. This method updates the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header will be carried over to the returned request.
You can opt-in to preserving the original state of the Host header by setting $preserveHost to true. When $preserveHost is set to true, this method interacts with the Host header in the following ways:
If the Host header is missing or empty, and the new URI contains a host component, this method will update the Host header in the returned request.
If the Host header is missing or empty, and the new URI does not contain a host component, this method will not update the Host header in the returned request.
If a Host header is present and non-empty, this method will not update the Host header in the returned request.