CodeBlanks Logo Icon CodeBlanks

PSR-15 basics

PHP course · Advanced · Lesson 72/92

Learn the PSR-15 middleware interface and the MiddlewareInterface.

PSR-15 MiddlewareInterface

PSR-15 is the standard middleware interface defined by PHP-FIG. MiddlewareInterface has a single method: process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface.

This standard lets middlewares written for different frameworks be used interchangeably. Every class has the same signature, so chaining is easy.

$request is a PSR-7 request, and $handler is the next request handler. Calling $handler->handle($request) continues the flow; the return value is a PSR-7 response.

How PSR-15 works

In a PSR-15 middleware class, the process method inspects the request, optionally logs or validates, then continues to the next step with $handler->handle($request).

The RequestHandlerInterface defines a single handle method. It represents either the next middleware or the innermost handler. Each call chains to the next.

In this lesson we mimic the PSR-15 structure with simplified classes: process and handle follow the same signature pattern without a PSR-7 dependency.

Exercise Write a PSR-15 style class

Define a LoggingMiddleware class that mimics the PSR-15 structure. Give it a public function process($request, $handler) method. First print echo "[LOG] Request received", then obtain the response with $response = $handler->handle($request), print echo "[LOG] Response produced" and return $response. FinalHandler is already defined. Instantiate the class, call process and print the response.

Show example solution
Write class LoggingMiddleware { public function process($request, $handler) { echo "[LOG] Request received\n"; $response = $handler->handle($request); echo "[LOG] Response produced\n"; return $response; } }. Then $mw = new LoggingMiddleware(); echo $mw->process("istek", new FinalHandler()) . "\n";.
Example solution:
<?php
class LoggingMiddleware {
    public function process($request, $handler) {
        echo "[LOG] Request received\n";
        $response = $handler->handle($request);
        echo "[LOG] Response produced\n";
        return $response;
    }
}
class FinalHandler {
    public function handle($request) {
        return "TAMAM";
    }
}
$mw = new LoggingMiddleware();
echo $mw->process("istek", new FinalHandler()) . "\n";

Test yourself

  1. The process method signature

    public function process(ServerRequestInterface $request, ____ $handler): ResponseInterface {
        return $handler->handle($request);
    }
    Show answer
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        return $handler->handle($request);
    }
  2. What does the PSR-15 process method return?

    • ResponseInterface
    • void
    • bool
    • array
    Show answer

    ResponseInterface — Correct. process returns a ResponseInterface, the PSR-7 response.

  3. Match the PSR-15 concepts

    MiddlewareInterface
    process() method
    RequestHandlerInterface
    handle() method
    ServerRequestInterface
    PSR-7 request
    ResponseInterface
    PSR-7 response

All lessons in Advanced