php – What is the purpose of the "request" and "response" objects of a web framework?

I've already had the opportunity to build frameworks in languages ​​such as PHP and Python, and I've also studied a lot about how their source code works.

Basically, we can understand that the idea of ​​frames is to abstract everything. And it's no different with the requisition and the answer.

Every request made by a web client goes through the request and response stage. And that 's the idea behind Silex, Slim and other frameworks.

Most frameworks used class Request to represent the request from the customer. This can vary between an object from the parameter of a method or factory, but in the end, the goal is always to represent the customer's request.

In it, it must contain important information for your system to work, such as:

  • Parameters of the query string or payload.
  • Content type requested via Content-Type or Accept.
  • Purchase request method, etc.

Of course, this information does not depend on the framework. They are available in PHP, for example, in the variable $ _SERVER with values ​​prefixed by HTTP_.

In turn, the class Reply greatly facilitates the abstraction of the response sent to the browser. It may seem simple, but when you think about a framework, you have to structure what is sent to the client very well. An example of this is the fact that the browser output can not be set before the headers. In the case, with the frame, you can for example call the definition of headers after defining the output result, because internally, the frame will stack the answer and organize how everything will be entered in the browser.