Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Zend Framework Blog:
Emitting Responses with Diactoros
Sep 15, 2017 @ 14:14:05

On the Zend Framework blog they've posted a tutorial from project lead Matthew Weier O'Phinney showing how to use the Diactoros package to create response emitters. These emitters are just methods of output related to HTTP handling (like headers, response codes and normal text output).

When writing middleware-based applications, at some point you will need to emit your response.

PSR-7 defines the various interfaces related to HTTP messages, but does not define how they will be used. Diactoros defines several utility classes for these purposes, including a ServerRequestFactory for generating a ServerRequest instance from the PHP SAPI in use, and a set of emitters, for emitting responses back to the client. In this post, we'll detail the purpose of emitters, the emitters shipped with Diactoros, and some strategies for emitting content to your users.

He starts by describing what "emitters" are in the context of PSR-7 applications and shows the code definition of the EmitterInterface, the base of all emitter classes. After covering some of the emitters that Diactoros includes he shows how to create one to emit file information and how you can mix and match them in a emitter "stack" to perform more that one operation on the output.

tagged: diactoros tutorial emitter response output example interface

Link: https://framework.zend.com/blog/2017-09-14-diactoros-emitters.html

Zend Framework Blog:
Specialized Response Implementations in Diactoros
Aug 24, 2017 @ 19:14:38

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.

tagged: tutorial diactoros package response example http zendframework

Link: https://framework.zend.com/blog/2017-08-24-diactoros-responses.html


Trending Topics: