Skip to content

rougin/onion

Repository files navigation

Onion

Latest Version on Packagist Software License Build Status Coverage Status Total Downloads

A collection of 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

Install the package using Composer:

$ composer require rougin/onion

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

Rougin\Onion\BodyParams

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

This middleware adds the necessary headers for Cross-Origin Resource Sharing (CORS). 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

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

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

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

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

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

See CHANGELOG for more recent changes.

Contributing

See CONTRIBUTING on how to contribute.

License

The MIT License (MIT). Please see LICENSE for more information.

About

HTTP middlewares for Slytherin.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors

Languages