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. [HTTP &amp; Networking](/categories/http)
4. /
5. rougin/onion

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

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

HTTP middlewares for Slytherin.

v0.1.1(1mo ago)01.3kMITPHPPHP &gt;=5.3.0CI passing

Since May 23Pushed 1mo agoCompare

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

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

Onion
=====

[](#onion)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5243c49113f064ed22a807f8cf39ff3a5e23b6113825cd8508b6191a369cebb5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f7567696e2f6f6e696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/onion)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rougin/onion/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d3b4a54658c6933c56986ba0a2d60ee2530660fbcd7c2af465ee0e59114d0876/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f7567696e2f6f6e696f6e2f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/rougin/onion/actions)[![Coverage Status](https://camo.githubusercontent.com/35576e665bbd63fb3b35fa5a2007198d66c14320cfffbed26be45847ff98cb63/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f7567696e2f6f6e696f6e3f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/rougin/onion)[![Total Downloads](https://camo.githubusercontent.com/5833d64f1af356305628c8895dbfb82e56a61d5bbb7675021d0a7b545db1cb93/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f7567696e2f6f6e696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/onion)

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

```
use Rougin\Slytherin\Application;

$app = new Application;

$app->add(new Rougin\Onion\BodyParams);
$app->add(new Rougin\Onion\CorsHeader);
$app->add(new Rougin\Onion\FormParser);
$app->add(new Rougin\Onion\JsonHeader);
$app->add(new Rougin\Onion\NullString);
$app->add(new Rougin\Onion\SpoofMethod);
$app->add(new Rougin\Onion\TrimString);

$app->run();
```

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();
```

### `Rougin\Onion\SpoofMethod`

[](#rouginonionspoofmethod)

This middleware allows overriding the HTTP method via a configurable key in the request body. By default, it reads the `_method` key to emulate `PUT`, `PATCH`, or `DELETE` requests from HTML forms, which only natively support `GET` and `POST`:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\SpoofMethod;

$app = new Application;

// Uses the default "_method" key ---
$app->add(new SpoofMethod);
// ----------------------------------

// Or specify a custom key ------
$key = '_http_method';

$app->add(new SpoofMethod($key));
// ------------------------------

$app->run();
```

### `Rougin\Onion\TrimString`

[](#rouginoniontrimstring)

This middleware trims whitespace from all string values in the request body and query parameters, including nested arrays:

```
// index.php

use Rougin\Slytherin\Application;
use Rougin\Onion\TrimString;

$app = new Application;

$app->add(new TrimString);

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

    // "  John  " will be trimmed to "John" ---
    $name = $data['name'];
    // ----------------------------------------

    return $response;
});

$app->run();
```

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

[](#change-log)

See [CHANGELOG](https://github.com/rougin/onion/blob/master/CHANGELOG.md) for more recent changes.

Contributing
------------

[](#contributing)

See [CONTRIBUTING](https://github.com/rougin/onion/blob/master/CONTRIBUTING.md) on how to contribute.

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](https://github.com/rougin/onion/blob/master/LICENSE.md) for more information.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance93

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity19

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.

###  Release Activity

Cadence

Every ~5 days

Total

2

Last Release

36d ago

### 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 (23 commits)")

---

Tags

middlewaresphp-middleware

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M82](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.9M6.9k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k114](/packages/httpsoft-http-message)[mezzio/mezzio-router

Router subcomponent for Mezzio

265.4M91](/packages/mezzio-mezzio-router)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)

PHPackages © 2026

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