PHP - why HTTP message implementations?

laukok

This may be a daft question but I have been seeing and reading HTTP messages or PSR-7 in the current trend on PHP for development. Why out of a sudden we need HTTP message implementations in PHP for the modern web development?

For instance, with Slim 3, now we are to do this,

$app->get('/hello/{name}', function ($request, $response, $args) {
    $response->write("Hello, " . $args['name']);
    return $response;
});

We now must 'wrap' the request's result in the HTTP response object.

What is wrong with this old classic way of responding the request below?

echo 'Hello, ' . $_GET('name');

It is easier to understand for me.

What are the benefits of implementing HTTP messages? When do we need to use them? And why?

marcosh

First thing, I'd recommend you to read section 3 of PSR-7 meta document.

I'll try to summarize some of the benefits of having a common set of interfaces for describing HTTP messages.

You could use superglobals variables (as $_GET and $_POST), but they are global mutable state. Along with this unit and integration testing of your code becomes hard.

For these reasons many PHP frameworks decided to implement an abstaction to represent HTTP messages (see for example Symfony HttpFoundation or Zend\Http).

This led to a situation where any application was based on a specific implementation of HTTP messages, so that it was hardly usable in projects built using other frameworks.

This is why a common set of interfaces helps abstract HTTP messages and work with them in a framework agnostic way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do browser implementations of HTTP/2 require TLS?

From Dev

Psr7 Http Message, why immutable?

From Dev

Why no overloaded constructor implementations in TypeScript?

From Dev

How to automatically bind implementations in PHP?

From Dev

Missing method implementations in PHP project

From Dev

How to set http response status code and message in php

From Dev

Why is "HTTP:..." a valid line of PHP code?

From Dev

Why should Map implementations override foreach?

From Dev

Why are there so many implementations of Object Pooling in Roslyn?

From Dev

Why are these two implementations of max in matlab different?

From Dev

Why is there a difference between two similar implementations of a 'for' loop?

From Dev

Why do we use `this` in CompareTo in Comparable implementations?

From Dev

Why do certain implementations run slow in Python?

From Dev

Why I get an error message "Notice: Undefined index: formats ..." in php?

From Dev

Easy UI implementations for Rails, coming from PHP

From Dev

Do current XHR implementations take advantage of HTTP/2?

From Dev

Why do we need both status code and phrase code in a http response message?

From Dev

Why do we need both status code and phrase code in a http response message?

From Dev

Why will php pthreads not work? http error internal server code 500.0

From Dev

Message Handlers and the Http Module?

From Dev

Why do different implementations of AES produce different output?

From Dev

Why most JavaScript native functions are slower than their naive implementations?

From Dev

Why can enum implementations not access private fields in the enum class

From Java

Why is there a dummy union member in some implementations of std::optional?

From Dev

Why are Python and Ruby so slow, while Lisp implementations are fast?

From Dev

Why insist all implementations of an interface extend a base class?

From Dev

Why are different default values permitted in implementations from those defined on interface?

From Dev

Why do STL implementations not use assertions to detect undefined behaviour?

From Dev

Why are implementations of Iterator<Item = T> and Iterator<Item = &T> conflicting?

Related Related

  1. 1

    Why do browser implementations of HTTP/2 require TLS?

  2. 2

    Psr7 Http Message, why immutable?

  3. 3

    Why no overloaded constructor implementations in TypeScript?

  4. 4

    How to automatically bind implementations in PHP?

  5. 5

    Missing method implementations in PHP project

  6. 6

    How to set http response status code and message in php

  7. 7

    Why is "HTTP:..." a valid line of PHP code?

  8. 8

    Why should Map implementations override foreach?

  9. 9

    Why are there so many implementations of Object Pooling in Roslyn?

  10. 10

    Why are these two implementations of max in matlab different?

  11. 11

    Why is there a difference between two similar implementations of a 'for' loop?

  12. 12

    Why do we use `this` in CompareTo in Comparable implementations?

  13. 13

    Why do certain implementations run slow in Python?

  14. 14

    Why I get an error message "Notice: Undefined index: formats ..." in php?

  15. 15

    Easy UI implementations for Rails, coming from PHP

  16. 16

    Do current XHR implementations take advantage of HTTP/2?

  17. 17

    Why do we need both status code and phrase code in a http response message?

  18. 18

    Why do we need both status code and phrase code in a http response message?

  19. 19

    Why will php pthreads not work? http error internal server code 500.0

  20. 20

    Message Handlers and the Http Module?

  21. 21

    Why do different implementations of AES produce different output?

  22. 22

    Why most JavaScript native functions are slower than their naive implementations?

  23. 23

    Why can enum implementations not access private fields in the enum class

  24. 24

    Why is there a dummy union member in some implementations of std::optional?

  25. 25

    Why are Python and Ruby so slow, while Lisp implementations are fast?

  26. 26

    Why insist all implementations of an interface extend a base class?

  27. 27

    Why are different default values permitted in implementations from those defined on interface?

  28. 28

    Why do STL implementations not use assertions to detect undefined behaviour?

  29. 29

    Why are implementations of Iterator<Item = T> and Iterator<Item = &T> conflicting?

HotTag

Archive