---
title: "Using the Middleware Pattern to Extend PHP Libraries"
url: https://daily.dev/posts/using-the-middleware-pattern-to-extend-php-libraries-4ujzqdndb
source_url: https://maximegosselin.com/posts/using-the-middleware-pattern-to-extend-php-libraries/
type: article
source: "Maxime Gosselin"
published: 2026-03-22T11:39:32.109Z
updated: 2026-03-22T11:39:54.273Z
tags: ["php", "design-patterns"]
reading_time: 7
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Using the Middleware Pattern to Extend PHP Libraries

**[Maxime Gosselin](https://daily.dev/sources/maximegosselin)** · 7 min read · 0 upvotes · 0 comments

## Summary

PSR-15's middleware pattern can be applied beyond HTTP to any PHP library component as a more flexible alternative to the Decorator pattern. The approach involves creating a middleware interface that mirrors the component interface with an added `$next` parameter, a delegator class to bridge the two, and using `array_reduce` to build the chain. This enables runtime composition, input/output modification, and short-circuiting — capabilities the Decorator pattern cannot cleanly express. A concrete HtmlRenderer example with caching and page title middlewares illustrates all three execution models: before, after, and both.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://maximegosselin.com/posts/using-the-middleware-pattern-to-extend-php-libraries/>

---

Tags: [#php](https://daily.dev/tags/php), [#design-patterns](https://daily.dev/tags/design-patterns)

[View this post on daily.dev](https://daily.dev/posts/using-the-middleware-pattern-to-extend-php-libraries-4ujzqdndb)
