QA
Senior LevelPHP 8+

How do PSR-7, PSR-15, and PSR-18 enable framework-agnostic middleware architecture?

❓ Interview Prompt: Explain PHP FIG standards: PSR-7 (HTTP Messages), PSR-15 (HTTP Handlers & Middleware), and PSR-18 (HTTP Client).

The PHP-FIG (Framework Interoperability Group) defines PSR (PHP Standard Recommendations) to enable framework-agnostic component sharing.

Core HTTP PSRs:

•
PSR-7 (HTTP Message Interfaces): Defines immutable 'ServerRequestInterface' and 'ResponseInterface' abstractions.
•
PSR-15 (HTTP Handlers & Middleware): Standardizes middleware pipeline execution:

'process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface'.

•
PSR-18 (HTTP Client): Standardizes sending HTTP requests via a unified client interface ('ClientInterface::sendRequest()').

Code Implementation Example

php
<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

// Standard PSR-15 Middleware implementation
class AuthMiddleware implements MiddlewareInterface {
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        if (!$request->hasHeader('Authorization')) {
            return new Nyholm\Psr7\Response(401, [], 'Unauthorized');
        }
        return $handler->handle($request); // Pass to next handler in pipeline
    }
}
💡Implementing a framework-agnostic PSR-15 HTTP middleware component.
🎯 Interview Key Takeaway

PSR-7, PSR-15, and PSR-18 standardize immutable HTTP messages and middleware, enabling interoperability across frameworks.

#PSR-7#PSR-15#Middleware#PHP-FIG#Senior
Back to all PHP 8+ Questions→

More PHP 8+ Interview Questions

Beginner LevelPHP

What is PHP and how does its shared-nothing request lifecycle work?

Beginner LevelPHP

What is the difference between echo and print in PHP?

Beginner LevelPHP

What is the difference between isset(), empty(), and is_null() in PHP?