Defining a PSR-7 Response class
The Response class represents outbound information returned to whatever entity made the original request. HTTP headers play an important role in this context as we need to know that format is requested by the client, usually in the incoming Accept
header. We then need to set the appropriate Content-Type
header in the Response class to match that format. Otherwise, the actual body of the response will be HTML, JSON, or whatever else has been requested (and delivered).
How to do it...
- The
Response
class is actually much easier to implement than theRequest
class as we are only concerned with returning the response from the server to the client. Additionally, it extends ourApplication\MiddleWare\Message
class where most of the work has been done. So, all that remains to be done is to define anApplication\MiddleWare\Response
class. As you will note, the only unique property is$statusCode
:namespace Application\MiddleWare; use Psr\Http\Message\ { Constants, ResponseInterface...