subjective-php/slim-options-middleware

Middleware to add an OPTIONS route to routes.

dev-master 2019-04-22 15:11 UTC

This package is auto-updated.

Last update: 2024-10-23 04:11:46 UTC


README

Middleware to add an OPTIONS route to existing routes.

Build Status Scrutinizer Code Quality Coverage Status

Latest Stable Version Latest Unstable Version License

Total Downloads Daily Downloads Monthly Downloads

Requirements

Requires PHP 7.0 (or later).

Composer

To add the library as a local, per-project dependency use Composer! Simply add a dependency on subjective-php/slim-options-middleware to your project's composer.json file such as:

composer require subjective-php/slim-options-middleware

Contact

Developers may be contacted at:

Project Build

With a checkout of the code get Composer in your PATH and run:

composer install
./vendor/bin/phpunit
./vendor/bin/phpcs

Slim 3 Example

require __DIR__ . '/vendor/autoload.php';

use SubjectivePHP\Slim\Middleware;

// This Slim setting is required for the middleware to work
$app = new Slim\App([
    "settings"  => [
        "determineRouteBeforeAppMiddleware" => true,
    ]
]);

// create the middleware
$optionsMiddleware = new Middleware\OptionsMiddleware('*', ['Authorization', 'Content-Type']);

$app->map(['GET', 'POST'], 'foos', function ($request, $response, $args) {
    return $response;
};

$app->add($optionsMiddleware);

$app->run();

Send an OPTIONS request to the API

curl -i -X OPTIONS http://example.org/foos

Response will be similar to

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=UTF-8
Date: Mon, 22 Apr 2019 12:45:18 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 0
Connection: keep-alive