subjective-php/slim-options-middleware

中间件,用于向路由中添加 OPTIONS 路由。

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

This package is auto-updated.

Last update: 2024-08-23 03:43:06 UTC


README

中间件,用于向现有路由中添加 OPTIONS 路由。

Build Status Scrutinizer Code Quality Coverage Status

Latest Stable Version Latest Unstable Version License

Total Downloads Daily Downloads Monthly Downloads

要求

需要 PHP 7.0(或更高版本)。

Composer

要将库添加为本地项目依赖,请使用 Composer!只需将 subjective-php/slim-options-middleware 添加到项目的 composer.json 文件中,例如:

composer require subjective-php/slim-options-middleware

联系

开发者可以通过以下方式联系:

项目构建

通过检出代码,将 Composer 添加到您的 PATH 中并运行

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

Slim 3 示例

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

向 API 发送 OPTIONS 请求

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

响应将类似于

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