PHPackages                             rougin/onion - 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. rougin/onion

ActiveLibrary

rougin/onion
============

HTTP middlewares for Slytherin.

0618PHP

Since Aug 24Pushed 8mo agoCompare

[ Source](https://github.com/rougin/onion)[ Packagist](https://packagist.org/packages/rougin/onion)[ RSS](/packages/rougin-onion/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Onion
=====

[](#onion)

A collection of [Slytherin](https://github.com/rougin/slytherin)-based HTTP middlewares.

Installation
------------

[](#installation)

Install the package using [Composer](https://getcomposer.org/):

```
$ composer require rougin/onion
```

Basic usage
-----------

[](#basic-usage)

To use any of the middlewares, they are needed to be added to the HTTP middleware stack in a `Slytherin` application:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\JsonHeader;

// Creates a new application instance ---
$app = new Application;
// --------------------------------------

// Adds the middleware to the application ---
$app->add(new JsonHeader);
// ------------------------------------------

// Runs the application ---
$app->run();
// ------------------------
```

Available middlewares
---------------------

[](#available-middlewares)

### `Rougin\Onion\BodyParams`

[](#rouginonionbodyparams)

This middleware parses the request body for complex HTTP methods such as `DELETE`, `PATCH`, and `PUT`. It supports both `application/x-www-form-urlencoded` and `multipart/form-data` content types. This is particularly useful because PHP does not automatically parse the request body for these HTTP methods:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\BodyParams;

$app = new Application;

$app->add(new BodyParams);

// ...

$app->run();
```

### `Rougin\Onion\CorsHeader`

[](#rouginonioncorsheader)

This middleware adds the necessary headers for [Cross-Origin Resource Sharing (CORS)](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing). It allows to specify which origins and HTTP methods are allowed to access in a application's resources:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\CorsHeader;

$app = new Application;

// Allows specified origins and methods ------
$origins = array('https://example.com');
$origins[] = 'https://api.example.com';

$methods = array('GET', 'POST', 'PUT');

$app->add(new CorsHeader($origins, $methods));
// -------------------------------------------

// ...

$app->run();
```

### `Rougin\Onion\FormParser`

[](#rouginonionformparser)

This middleware parses the request body from `php://input`. It can handle both JSON and `form-urlencoded` data. This is useful for APIs that receive data in the request body:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\FormParser;

$app = new Application;

$app->add(new FormParser);

// ...

$app->run();
```

### `Rougin\Onion\JsonHeader`

[](#rouginonionjsonheader)

This middleware sets the `Content-Type` header of the response to `application/json` if it has not been set already. This is a convenient way to ensure that the application always returns JSON responses:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\JsonHeader;

$app = new Application;

$app->add(new JsonHeader);

$app->get('/users', function ($request, $response)
{
    $users = array();

    $users[] = array('id' => 1, 'name' => 'John Doe');
    $users[] = array('id' => 2, 'name' => 'Jane Doe');

    return $response->withJson($users);
});

$app->run();
```

### `Rougin\Onion\NullString`

[](#rouginonionnullstring)

This middleware converts empty strings, `"null"`, and `"undefined"` values in the request data to `null`. This can be useful for cleaning up input data before it is processed by the application:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\NullString;

$app = new Application;

$app->add(new NullString);

$app->post('/articles', function ($request, $response)
{
    $data = $request->getParsedBody();

    // Will be "null" if the input is an empty ---
    $author = $data['author'];
    // -------------------------------------------

    return $response;
});

$app->run();
```

Change log
----------

[](#change-log)

See [CHANGELOG](CHANGELOG.md) for more recent changes.

Development
-----------

[](#development)

Includes tools for code quality, coding style, and unit tests.

### Code quality

[](#code-quality)

Analyze code quality using [phpstan](https://phpstan.org/):

```
$ phpstan
```

### Coding style

[](#coding-style)

Enforce coding style using [php-cs-fixer](https://cs.symfony.com/):

```
$ php-cs-fixer fix --config=phpstyle.php
```

### Unit tests

[](#unit-tests)

Execute unit tests using [phpunit](https://phpunit.de/index.html):

```
$ composer test
```

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance42

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c7721589a958a1fbcef1b527c794d820d75b82988f14b2ed86a1c6904d76a59e?d=identicon)[rougin](/maintainers/rougin)

---

Top Contributors

[![rougin](https://avatars.githubusercontent.com/u/6078637?v=4)](https://github.com/rougin "rougin (3 commits)")

---

Tags

middlewaresphp-middleware

### Embed Badge

![Health badge](/badges/rougin-onion/health.svg)

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

PHPackages © 2026

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