PSR-7 is a PHP Standard Recommendation (PSR) that focuses on HTTP messages in PHP. It was developed by the PHP-FIG (Framework Interoperability Group) and defines interfaces for working with HTTP messages, as used by web servers and clients.
Request and Response:
PSR-7 standardizes how HTTP requests and responses are represented in PHP. It provides interfaces for:
Immutability:
All objects are immutable, meaning that any modification to an HTTP object creates a new object rather than altering the existing one. This improves predictability and makes debugging easier.
Streams:
PSR-7 uses stream objects to handle HTTP message bodies. The StreamInterface defines methods for interacting with streams (e.g., read()
, write()
, seek()
).
ServerRequest:
The ServerRequestInterface extends the RequestInterface to handle additional data such as cookies, server parameters, and uploaded files.
Middleware Compatibility:
PSR-7 serves as the foundation for middleware architectures in PHP. It simplifies the creation of middleware components that process HTTP requests and manipulate responses.
PSR-7 is widely used in modern PHP frameworks and libraries, including:
The goal of PSR-7 is to improve interoperability between different PHP libraries and frameworks by defining a common standard for HTTP messages.