PHPackages                             noglitchyo/middleware-collection-request-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. noglitchyo/middleware-collection-request-handler

ActiveLibrary[HTTP &amp; Networking](/categories/http)

noglitchyo/middleware-collection-request-handler
================================================

Simple PSR-15 middleware collection request handler.

2.0.1(6y ago)78.4k↓50%11MITPHPPHP &gt;=7.3

Since Jun 23Pushed 6y ago1 watchersCompare

[ Source](https://github.com/noglitchyo/middleware-collection-request-handler)[ Packagist](https://packagist.org/packages/noglitchyo/middleware-collection-request-handler)[ RSS](/packages/noglitchyo-middleware-collection-request-handler/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (6)Versions (5)Used By (1)

middleware-collection-request-handler
=====================================

[](#middleware-collection-request-handler)

Lightweight &amp; dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.

[![PHP from Packagist](https://camo.githubusercontent.com/8c9eaf928b3352715e9cae5384945a41b403aad910549dbcfb13ebc63c8e272a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e737667)](https://camo.githubusercontent.com/8c9eaf928b3352715e9cae5384945a41b403aad910549dbcfb13ebc63c8e272a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e737667)[![Build Status](https://camo.githubusercontent.com/539053283a79d3e7bc77e177d491a0158f63522a16fcc723cdd4fb8eb9d5016d/68747470733a2f2f7472617669732d63692e6f72672f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/noglitchyo/middleware-collection-request-handler)[![codecov](https://camo.githubusercontent.com/65b9b5becf42b4824f622c7bdc6e13385ac67c7f8822db6f20c1d9bab0f68fd8/68747470733a2f2f636f6465636f762e696f2f67682f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/noglitchyo/middleware-collection-request-handler)[![Scrutinizer code quality (GitHub/Bitbucket)](https://camo.githubusercontent.com/1fff302e08fab71ca92d8c671b3f1bbe6a11597340bf8ad8d85ba850ff16bd07/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e737667)](https://camo.githubusercontent.com/1fff302e08fab71ca92d8c671b3f1bbe6a11597340bf8ad8d85ba850ff16bd07/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e737667)[![Packagist](https://camo.githubusercontent.com/37651ffed137084725efbb1bdafc8d758d60c8b2a3f5030feb5e581dc3bde625/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e737667)](https://camo.githubusercontent.com/37651ffed137084725efbb1bdafc8d758d60c8b2a3f5030feb5e581dc3bde625/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6e6f676c69746368796f2f6d6964646c65776172652d636f6c6c656374696f6e2d726571756573742d68616e646c65722e737667)

### Description

[](#description)

PSR-15 request handler implementing both [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php) and [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php)able to manage a collection of middlewares implementing the [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php).

It can be used either as a RequestHandler or as a Middleware to fit into any implementation.

Comes with a set of middleware collections using different strategy (LIFO, FIFO...) on how the middlewares from the collection are provided to the RequestHandler, and also provides a simple MiddlewareCollectionInterface to implement in a glimpse your own strategy.

### Goals

[](#goals)

- Simplicity
- Interoperability

### Getting started

[](#getting-started)

#### Requirements

[](#requirements)

- PHP &gt;= 7.3

#### Installation

[](#installation)

`composer require noglitchyo/middleware-collection-request-handler`

#### Run

[](#run)

Create a new instance of the request handler class which can be use as a request handler or as a middleware.

##### From the constructor

[](#from-the-constructor)

`RequestHandler::__construct(MiddlewareCollectionInterface $middlewareCollection, RequestHandlerInterface $defaultRequestHandler = null)`

- ***`$middlewareCollection`*** : [MiddlewareCollectionInterface](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/MiddlewareCollectionInterface.php)

    Contains the middlewares and defines the strategy used to store the middlewares and to retrieve the next middleware. Some implementations with common strategies are provided: [stack (LIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplStackMiddlewareCollection.php), [queue (FIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplQueueMiddlewareCollection.php).
- ***`$defaultRequestHandler = null`*** : [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php)

    Provides a default response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php) if none of the middlewares in the collection was able to create one.

    Some examples of what could be a "default request handler":

    - with the [ADR pattern](https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder), the default request handler might be your action class.\*
    - with the [MVC pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), the default request handler might be the action method of your controller.

##### From the factory method

[](#from-the-factory-method)

`RequestHandler::fromCallable(callable $callable, MiddlewareCollectionInterface $middlewareCollection)`

It creates a RequestHandler instance by wrapping the given `callable` inside an anonymous instance of RequestHandlerInterface. The callable is the $defaultRequestHandler. **It MUST returns a response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php).**

##### Example

[](#example)

Below, this is how simple it is to get the middleware handler running:

```
