PSR-7- Does changing the Request effect the Response?

Assuming I was to send a Request to a browser:

POST /hello HTTP/1.1
Host: example.com

message=world

In order to get a response message back I would need to use a Router/Dispatcher to find the given route handler/callback and then find the relavent template/function and populate the message body with the output.

Considering you pass both the Request and the Response to Middleware, if you where to make a change to the Request - i.e. change the target and pass it to the next middleware the response for this request would still be the same, you will still have a response from the dispatched route of the original request.

What happens to the response if the request is changed? Do you have to pass it through the Router for each request or is it a case of once a Route has been dispatched any changes to the original request and null void and have no bearing on the response, instead you should just be editing the response itself?

The request shouldn’t really ever be modified. From the Interface example code.

Requests are considered immutable; all methods that might change state MUST

  • be implemented such that they retain the internal state of the current
  • message and return an instance that contains the changed state.

Basically that means to me, you can only add additional information to the request. You shouldn’t be changing the original request at all.

That doesn’t really answer your question, but might get you looking towards different solutions to your problem, which I actually don’t understand. LOL! :smiley:

Scott

They are immutable, but looking at a couple of implementations they can be changed they just return a new instance (clone) of the request

So instead of:

public function withMethod($method){
  $this->method = $method;
  return $this
} 

you would have something like:

public function withMethod($method){
  $new = clone $this;
  $new->method = $method;
  return $new
} 

Preserving the state of the old request but also providing you with a new one.

I still have no idea what you are asking. Are you concerned about server side requests?

Scott

I’m asking how to implement PSR-7 Request->Response Objects.

If you are bubbling a request + response through middleware stack but middleware can’t actually change the request why bother passing it through in the first place. If a request can change how is this change reflected in the response when say you define a new method (and as such change what route/controller should be called).

E.g:
If I was to make a request /foo, I might get a handler controller::method, inside that method might be a twig template returned as a response. Now If I was to use middleware to change the httpmethod from GET to POST this might be a different route/controller. How is this change reflected, how would the response update?

What reasons would there be to change a request method from a GET to a POST? Sorry, but I am still not understanding your use case or needs.

If you don’t want to pass around an object, sounds like it would be a good opportunity to use a DI/ service container.

Scott

Not a great example. In symfony you have Master and Sub Requests. If you where to think of it like Symfony I guess I’m askin how would you handle the Sub requests, i.e. how do you translate the Request Object into a new Response object once something is changed via Middleware after the route has been dispatched.

I think I have found my answer by looking at the PSR-Bridge for Symfony.

In Symfony, when you make a sub-request, you create a new request object and denote it as a sub-request.The “master” request, as you called it remains unchanged.

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.