Expressive 3 ResponseInterface service return response factory not response prototype/origin

Hi, guys!

Why does ResponseInterface::class service choose return response factory, not response prototype/origin?
And some construct methods use __construct(callable $responseFactory) not __construct(ResponseInterface $responsePrototype) .

Such as:

  • Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware
  • Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware
  • Zend\Stratigility\Middleware\ErrorHandler
  • etc.

Because of lazy?

This was actually mentioned in : https://framework.zend.com/blog/2018-02-27-expressive-3-rc1.html . Basically helps to make use of other PSR-7 libraries other than diactoros. And when PSR-17 is finalized you can easily switch to make use of it.

1 Like

Besides what Harikt is writing, there were issues when using Expressive on top of async frameworks. The response body is mutable and in case of an exception, that body was reused and displaying older errors too. By using a ResponseFactory it’s safe to share via the container and you always have a complete new response.

1 Like