On the Zend Framework blog today Matthew Weier O'Phinney has written up a post showing how to implement specialized responses in Diactoros, a PHP package that makes it easier to work more directly with the request and response in your application.
When writing PSR-7 middleware, at some point you'll need to return a response.Maybe you'll be returning an empty response, indicating something along the lines of successful deletion of a resource. Maybe you need to return some HTML, or JSON, or just plain text. Maybe you need to indicate a redirect.
But here's the problem: a generic response typically has a very generic constructor. [...] Fortunately, Diactoros provides a number of convenience implementations to help simplify the most common use cases.
He then spends the rest of the article showing some example responses types and the code to make them work:
- EmptyResponse
- RedirectResponse
- TextResponse
- HtmlResponse
- JsonResponse
Each one is included as a part of the Diactoros package. For more information, check out the documentation for the package and accompanying examples.