Closed akrabat closed 7 years ago
The first part sounds alot like zend-diactoros: https://github.com/zendframework/zend-diactoros/tree/master/src/Response
Can you please expand on why the response object shouldn't be modified on its way through middleware stack? I actually find it a very interesting feature - e.g. setting default headers to the response that can be overridden in the route callable.
@jirkahronik That is the design decision of the PHP-FIG.
This becomes moot with the proposed PSR for HTTP Middleware.
There's no real need for a
$response
parameter in the route callable as it shouldn't be modified on the way in anyway.As such, the user should create a new Response object in the route callable to return. Fortunately Slim's
Response
object doesn't have any required parameters, so this is easy:We could also handle specialised Responses with new classes rather than more
with
methods onResponse
:return new JsonResponse($data, $status=200);
return new RedirectResponse($url, $status=302);
return new NotFoundResponse();
Rendering views via templates
For HTML output, it's convenient to use a template engine such a Twig, Plates or a separate PHP file. For these cases, I suggest we use "View" classes with a render() method that returns a Response object:
return (new TwigView())->render('home.twig', $data);
return (new PhpView())->render('home.phtml', $data);