PHPackages                             relay/middleware - 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. relay/middleware

AbandonedArchivedLibrary

relay/middleware
================

Relay-compatible middleware decorators.

1.2.0(9y ago)39261.4k↑10.1%9[1 issues](https://github.com/relayphp/Relay.Middleware/issues)[1 PRs](https://github.com/relayphp/Relay.Middleware/pulls)12MITPHP

Since Jul 23Pushed 8y ago2 watchersCompare

[ Source](https://github.com/relayphp/Relay.Middleware)[ Packagist](https://packagist.org/packages/relay/middleware)[ Docs](https://github.com/relayphp/Relay.Middleware)[ RSS](/packages/relay-middleware/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (12)

Relay Middleware
================

[](#relay-middleware)

This package include the following Relay-compatible middleware:

- *ExceptionHandler* to handle exceptions from subsequent middleware
- *FormContentHandler* to deserialize the URL-encoded payload of a PSR-7 request
- *JsonContentHandler* to deserialize the JSON payload of a PSR-7 request
- *JsonDecoder* to deserialize the JSON payload of a PSR-7 request (**deprecated**)
- *ResponseSender* to send a PSR-7 response
- *SessionHeadersHandler* to manage session headers "manually", instead of PHP managing them automatically
- *StatelessExceptionHandler* to handle exceptions from subsequent middleware (suitable for multiple request/response cycles)

This package is installable and PSR-4 autoloadable via Composer as `relay/middleware`.

ExceptionHandler
----------------

[](#exceptionhandler)

Similarly, the *ExceptionHandler* does what it sound like: it catches any exceptions that bubble up through the subsequent middleware decorators.

The *ExceptionHandler* does nothing with the `$request` or `$response`, and passes them directly to `$next` inside a `try/catch` block. If no exception bubbles up, it returns the `$response` from `$next`. However, if it catches an exception, it returns an entirely new `$response` object with the exception message and an HTTP 500 status code. It then returns the new `$response` object.

The *ExceptionHandler* is intended to go near the top of the Relay queue, but after the *ResponseSender*, so that the *ResponseSender* can then send the returned `$response`.

To add the *ExceptionHandler* to your queue, instantiate it directly with an empty $response implementation object ...

```
$queue[] = new \Relay\Middleware\ExceptionHandler(new ResponseImplementation());
```

... or use a `$resolver` of your choice to instantiate it from the `$queue`.

FormContentHandler
------------------

[](#formcontenthandler)

*FormContentHandler* works almost identically to *JsonContentHandler* (below), but parses payloads of requests that have `application/x-www-form-urlencoded` as the `Content-Type`.

JsonContentHandler
------------------

[](#jsoncontenthandler)

Again, the *JsonContentHandler* does what it sounds like: it deserializes the JSON payload of a PSR-7 request object and makes the parameters available in subsequent middleware decorators.

The *JsonContentHandler* checks the incoming request for a method other than `GET`and for an `application/json` or `application/vnd.api+json` `Content-Type` header. If it finds both of these, it parses the JSON and makes it available as the *parsed body* of the `$request` before passing it and the `$response` to `$next`. If the method is `GET` or the `Content-Type` header defines a different mime type, the *JsonContentHandler* ignores the `$request` and continues the chain.

To add the *JsonContentHandler* to your queue, instantiate it directly...

```
$queue[] = new \Relay\Middleware\JsonContentHandler();
```

... or use a `$resolver` of your choice to instantiate it from the `$queue`.

To access the decoded parameters in subsequent middleware, use the `getParsedBody()` method of the `$request`

```
$decodedJsonData = $request->getParsedBody();
```

JsonDecoder
-----------

[](#jsondecoder)

**NOTE: This handler has been deprecated in favor of *JsonContentHandler*!**

Again, the *JsonDecoder* does what it sounds like: it deserializes the JSON payload of a PSR-7 request object and makes the parameters available in subsequent middleware decorators.

The *JsonDecoder* checks the incoming request for a method other than `GET` and for an `application/json` `Content-Type` header. If it finds both of these, it decodes the JSON and makes it available as the *parsed body* of the `$request`before passing it and the `$response` to `$next`. If the method is `GET` or the `Content-Type` header does not specify `application/json`, the *JsonDecoder*does nothing with the `$request` and passes it and the `$response` to `$next`.

To add the *JsonDecoder* to your queue, instantiate it directly...

```
$queue[] = new \Relay\Middleware\JsonDecoder();
```

... or use a `$resolver` of your choice to instantiate it from the `$queue`.

To access the decoded parameters in subsequent middleware, use the `getParsedBody()` method of the `$request`

```
$decodedJsonData = $request->getParsedBody();
```

ResponseSender
--------------

[](#responsesender)

The *ResponseSender* does just what it sounds like: it sends the PSR-7 response object.

The *ResponseSender* does nothing with the `$request` or `$response`, passing them immediately to `$next`. Afterwards, it takes the returned `$response` and sends it using `header()` and `echo`, and returns the sent `$response`.

The *ResponseSender* is intended to go at the top of the Relay queue, so that it is the middleware with the last opportunity to do something with the returned response.

To add the *ResponseSender* to your Relay queue, instantiate it directly ...

```
$queue[] = new \Relay\Middleware\ResponseSender();
```

... or use a `$resolver` of your choice to instantiate it from the `$queue`.

SessionHeadersHandler
---------------------

[](#sessionheadershandler)

Normally, PHP will send out headers for you automatically when you call `session_start()`. However, this means the headers are not being sent as part of the PSR-7 response object, and are thus outside your control. This handler puts them back under your control by placing the relevant headers in the PSR-7 response; its behavior is almost identical to the native PHP automatic session headers behavior.

> NOTE: For this middleware to work, you **must** disable the PHP session header management ini settings. For example:
>
> ```
> ini_set('session.use_trans_sid', false);
> ini_set('session.use_cookies', false);
> ini_set('session.use_only_cookies', true);
> ini_set('session.cache_limiter', '');
>
> ```
>
>
>
> If you do not, the handler will throw a RuntimeException.

To add the *SessionHeadersHandler* to your queue, instantiate it directly...

```
$queue[] = new \Relay\Middleware\SessionHeadersHandler();
```

... or use a `$resolver` of your choice to instantiate it from the `$queue`.

When instantiating, you can pass a [cache limiter](http://php.net/session_cache_limiter) value as the first constructor parameter. The allowed values are 'nocache', 'public', 'private\_no\_cache', or 'private'. If you want no cache limiter header at all, pass an empty string ''. The default is 'nocache'.

You can also pass a [cache expire](http://php.net/session_cache_expire) value, in minutes, as the second constructor parameter. The default is 180 minutes.

StatelessExceptionHandler
-------------------------

[](#statelessexceptionhandler)

The *StatelessExceptionHandler* behaves the same as the *ExceptionHandler*. The difference is that it is suitable for use in environments where it may be used multiple times.

To add the *StatelessExceptionHandler* to your queue, instantiate it directly with a factory that can be used to create $response objects ...

```
$responseFactory = function () {
    return new ResponseImplementation();
};

$queue[] = new \Relay\Middleware\StatelessExceptionHandler($responseFactory);
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor3

3 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~244 days

Total

5

Last Release

2974d ago

Major Versions

0.1.0 → 1.0.02016-02-22

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/25754?v=4)[Paul M. Jones](/maintainers/pmjones)[@pmjones](https://github.com/pmjones)

![](https://www.gravatar.com/avatar/7358764d4fa43428d870fd432df278f299f095b8612c15394ed89ed45acc6688?d=identicon)[kevinsmith](/maintainers/kevinsmith)

---

Top Contributors

[![cgray](https://avatars.githubusercontent.com/u/3750843?v=4)](https://github.com/cgray "cgray (6 commits)")[![cxj](https://avatars.githubusercontent.com/u/446131?v=4)](https://github.com/cxj "cxj (4 commits)")[![rpalladino](https://avatars.githubusercontent.com/u/1429151?v=4)](https://github.com/rpalladino "rpalladino (4 commits)")[![AndrewCarterUK](https://avatars.githubusercontent.com/u/6486835?v=4)](https://github.com/AndrewCarterUK "AndrewCarterUK (2 commits)")[![bryanagee](https://avatars.githubusercontent.com/u/290124?v=4)](https://github.com/bryanagee "bryanagee (2 commits)")[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (1 commits)")[![elazar](https://avatars.githubusercontent.com/u/15487?v=4)](https://github.com/elazar "elazar (1 commits)")[![jakejohns](https://avatars.githubusercontent.com/u/174708?v=4)](https://github.com/jakejohns "jakejohns (1 commits)")[![pmjones](https://avatars.githubusercontent.com/u/25754?v=4)](https://github.com/pmjones "pmjones (1 commits)")

### Embed Badge

![Health badge](/badges/relay-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/relay-middleware/health.svg)](https://phpackages.com/packages/relay-middleware)
```

###  Alternatives

[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

536204.9M23](/packages/league-uri-interfaces)[neuron-core/neuron-ai

The PHP Agentic Framework.

1.8k245.3k21](/packages/neuron-core-neuron-ai)[simplesamlphp/saml2

SAML2 PHP library from SimpleSAMLphp

30317.2M40](/packages/simplesamlphp-saml2)[php-heroku-client/php-heroku-client

A PHP client for the Heroku Platform API

24404.8k4](/packages/php-heroku-client-php-heroku-client)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
